class DeadLock{
public static void main(String[] args){
TestDeadLock a=new TestDeadLock(true);
TestDeadLock b=new TestDeadLock(false);
Thread t1=new Thread(a);
Thread t2=new Thread(b);
t1.start();
t2.start();
}
}
class TestDeadLock implements Runnable{
private boolean flag;
TestDeadLock(boolean flag){
this.flag=flag;
}
public void run(){
if (flag==true){
synchronized(MyLock.lock1){
System.out.println(Thread.currentThread().getName()+"...if lock1");
synchronized(MyLock.lock2){
System.out.println(Thread.currentThread().getName()+"...if lock2");
}
}
}
else{
synchronized(MyLock.lock2){
System.out.println(Thread.currentThread().getName()+"...else lock2");
synchronized(MyLock.lock1){
System.out.println(Thread.currentThread().getName()+"...else lock1");
}
}
}
}
}
class MyLock{
static final Object lock1=new Object();
static final Object lock2=new Object();
}
死锁举例
最新推荐文章于 2022-10-12 13:24:06 发布