public class DeadLockDemo {
public static void main(String[] args) {
Thread t1=new Thread(new TestDeadLock(true));
Thread t2=new Thread(new TestDeadLock(false));
t1.start();
t2.start();
}
}
class TestDeadLock implements Runnable{
private boolean flag;
public TestDeadLock(boolean flag) {
this.flag=flag;
}
@Override
public void run() {
if (flag) {
synchronized (MyLock.locka) {
System.out.println("if locka");
synchronized (MyLock.lockb) {
System.out.println("if lockb");
}
}
}else{
synchronized (MyLock.lockb) {
System.out.println("else lockb");
synchronized (MyLock.locka) {
System.out.println("else locka");
}
}
}
}
}
class MyLock{
static Object locka=new Object();
static Object lockb=new Object();
} |
|