public class MyLock {
public static Object objA = new Object();
public static Object objB = new Object();
}
public class DieLock extends Thread{
private boolean flag;
public DieLock(boolean flag){
this.flag = flag;
}
@Override
public void run() {
if(flag){
synchronized(MyLock.objA){
System.out.println("if objA");
synchronized (MyLock.objB) {
System.out.println("if objB");
}
}
}else{
synchronized (MyLock.objB) {
System.out.println("else objB");
synchronized (MyLock.objA) {
System.out.println("else objA");
}
}
}
}
}
public class DieLockDemo {
public static void main(String[] args) {
DieLock dl1 = new DieLock(true);
DieLock dl2 = new DieLock(false);
dl1.start();
dl2.start();
}
}