/**
多线程--写出一个死锁程序
*/
class Test implements Runnable
{
private boolean flag;
public Test(boolean flag){
this.flag = flag;
}
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
{
public static Object locka = new Object();
public static Object lockb = new Object();
}
class DeadLockTest
{
public static void main(String[] args)
{
Thread t1 = new Thread(new Test(false));
Thread t2 = new Thread(new Test(true));
t1.start();
t2.start();
}
}