死锁

public class cs {
public static void main(String[] args) {
String b1="第一把锁";
String b2="第二把锁";
String b3="第三把锁";
si s1=new si(b1,b2,"h");
si s2=new si(b2,b3,"h");
si s3=new si(b3,b1,"h");
s1.start();
s2.start();
s3.start();
}
}
class si extends Thread{
private Object r1;
private Object r2;
private String name;
si(Object r1,Object r2,String name){
this.r1=r1;
this.r2=r2;
this.setName(name);
}
public void run(){
synchronized (r1) {
System.out.println(this.getName()+"得到线程1 ----"+r1);
try{
sleep(100);
}catch(Exception e){}
synchronized(r2){
System.out.println(this.getName()+"得到线程2----"+r2);
System.out.println("得到两个 执行完毕");
}

}
}
}

posted @ 2017-12-25 10:28  zhouwen周文  阅读(70)  评论(0编辑  收藏  举报