________________死锁

//简单易懂的死锁程序

public class Lock implements Runnable {
Boolean flag;

public Lock(boolean flag){
this.flag=flag;
}
@Override
public void run() {
while(true){


if(flag)
{
synchronized(Mylock.locka){
yield();
System.out.println(Thread.currentThread()
.getName()+"if__________locka");
synchronized(Mylock.lockb){
System.out.println(Thread.currentThread()
.getName()+"if__________lockb");
}
}

}
else{
synchronized(Mylock.lockb){
yield();
System.out.println(Thread.currentThread()
.getName()+"else__________locka");
synchronized(Mylock.locka){
System.out.println(Thread.currentThread()
.getName()+"else__________lockb");
}
}
}
}
}
private void yield() {
// TODO Auto-generated method stub

}
static class Mylock{
public static final Object locka=new Object();
public static final Object lockb=new Object();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Lock a=new Lock(false);
Lock b=new Lock(true);
Thread t1=new Thread(a);
Thread t2=new Thread(b);
t1.start();
t2.start();

}

 

}

posted @ 2017-11-02 00:00  637  阅读(83)  评论(0编辑  收藏  举报