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 {
private boolean flag;
Object obj1 = new Object();
Object obj2 = new Object();
xiancheng(boolean flag) {
this.flag = flag;
}
public void run() {
if(flag) {
while(true){
synchronized(obj1) {
System.out.println("if-----obj1");
synchronized(obj2) {
System.out.println("if-----obj2");
}
}
}
}
else {
while(true){
synchronized(obj2) {
System.out.println("-----obj2");
synchronized(obj1) {
System.out.println("else-----obj1");
}
}
}
}
}
}
为什么这两个锁不上 别告诉我怎么改 我知道怎么改 我就想知道为什么? |