package thread;
/**
*线程的死锁:线程同步中嵌套同步,锁却不同
* @author zjw
*
*/
public class DeadLock {
public static void main(String[] args) {
Dead1 d=new Dead1(true);
Dead1 d1=new Dead1(false);
new Thread(d).start();
new Thread(d1).start();
}
}
class Dead1 implements Runnable{
boolean boo;
public Dead1(boolean boo){
this.boo=boo;
}
public void run(){
if(boo){
synchronized(Lock.o1){
System.out.println(" if o1_lock");
synchronized(Lock.o2){
System.out.println(" if o2_lock");
}
}
}else{
synchronized(Lock.o2){
System.out.println(" else o2_lock");
synchronized(Lock.o1){
System.out.println(" else o1_lock");
}
}
}
}
}
/**
* 创建不同的对象锁
* @author zjw
*
*/
class Lock{
static Object o1=new Object();
static Object o2=new Object();
}