class MyLock{
static Object locka = new Object();
static Object lockb = new Object();
}
class Test2 implements Runnable{
private boolean flag;
Test2(boolean flag){
this.flag = flag;
}
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("if lockb");
synchronized(MyLock.locka){
System.out.println("if locka");
}
}
}
}
}
}
public class DeadLockTest {
public static void main(String[] args) {
Thread t1 = new Thread(new Test2(true));
Thread t2 = new Thread(new Test2(false));
t1.start();
t2.start();
}
}
死锁:同步中嵌套同步
|
|