本帖最后由 赵学刚 于 2012-12-3 12:58 编辑
/**
* 死锁测试
*/
public class test6 implements Runnable {
private boolean flag;
test6(boolean flag){
this.flag=flag;
}
@Override
public void run() {
if(flag){ while(true){
synchronized(MyLock.locka){
System.out.println("if locka");
synchronized(MyLock.lockb){
System.out.println("if lockb");
}
}
}
}
else{ while(true){
synchronized(MyLock.lockb){
System.out.println("else lockb");
synchronized(MyLock.locka){
System.out.println("else locka");
}
}
}
}
}
static class MyLock {
static Object locka=new Object();
static Object lockb=new Object();
}
public static void main(String[] args) {
Test6 t=new Test6(true);
Thread t1=new Thread(t);
Thread t2=new Thread(t);
t1.start();
t2.start();
}
}
为什么不会死锁,而是一直打印不会停呢
|