public class DidSynchronized {
public static void main(String[] args) {
Thread a = new Thread(new xiancheng(true));
Thread b = new Thread(new xiancheng(false));
a.start();
b.start();
}
}
class xiancheng implements Runnable {
static boolean flag;
xiancheng(boolean flag) {
this.flag = flag;
}
public void run() {
if(flag) {
while(true){
synchronized(Mylock.obj1) {
System.out.println("if-----obj1");
synchronized(Mylock.obj2) {
System.out.println("if-----obj2");
}
}
}
}
else {
while(true){
synchronized(Mylock.obj2) {
System.out.println("-----obj2");
synchronized(Mylock.obj1) {
System.out.println("else-----obj1");
}
}
}
}
}
}
class Mylock {
static Object obj1 = new Object();
static Object obj2 = new Object();
}
这个死锁为什么锁不上 谁帮看下 |