大家看看这程序有没有死锁呢?
运行结果为:
if locka
if lockb
else locka
else lockb
package cn.heima.deadlockdemo;
class Test implements Runnable{
private boolean flag;
Test(Boolean flag){
this.flag = flag;
}
public void run(){
if(flag){
synchronized(Lock.lockA){
System.out.println("if locka");
synchronized(Lock.lockB){
System.out.println("if lockb");
}
}
} else {
synchronized(Lock.lockA){
System.out.println("else locka");
synchronized(Lock.lockB){
System.out.println("else lockb");
}
}
}
}
}
class Lock{
static Object lockA = new Object();
static Object lockB = new Object();
}
public class DeadLockTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Thread t1 = new Thread(new Test(true));
Thread t2 = new Thread(new Test(false));
t1.start();
t2.start();
}
} |
|