package com.what21.thread06;
public class DieLockThreadTest {
/**
* @param args
*/
public static void main(String[] args) {
DieLock dl1 = new DieLock();
DieLock dl2 = new DieLock();
dl1.flag = 1;
dl2.flag = 0;
Thread t1 = new Thread(dl1);
Thread t2 = new Thread(dl2);
t1.start();
t2.start();
}
}
class DieLock implements Runnable {
public int flag = 1;
static Object o1, o2;
static {
o1 = new Object();
o2 = new Object();
}
public void run() {
System.out.println("flag=" + this.flag);
if (flag == 1) {
synchronized (o1) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (o2) {
System.out.println("... 1 ...");
}
}
}
if (flag == 0) {
synchronized (o2) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (o1) {
System.out.println("... 0 ...");
}
}
}
}
}