package thread;
/**
* JAVA
* @author hero
*
*/
class TestLock implements Runnable {
private boolean flag;
TestLock(boolean flag){
this.flag = flag;
}
public void run() {
if(flag){
while(true){
synchronized (MyLock.locka) {
// A -- B
System.out.println("if locka");
synchronized (MyLock.lockb) {
System.out.println("if lockb");
}
}
}
}else{
while(true){
synchronized (MyLock.lockb) {
// B -- A
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();
}
public class DeadLockTest8 {
/**
* @param args
*/
public static void main(String[] args) {
Thread t1 = new Thread(new TestLock(true));// A -- B
Thread t2 = new Thread(new TestLock(false));// B -- A
t1.start();
t2.start();
}
}