import javax.print.attribute.standard.ReferenceUriSchemesSupported;
/**
* Created by patkritLee on 2017/1/1.
*/
class Lock{
static Object obj1 = new Object();
static Object obj2 = new Object();
}
class Dead implements Runnable{
private boolean flag;
Dead(boolean flag){
this.flag = flag;
}
public void run(){
if(flag) {
while(true) {
synchronized (Lock.obj1) {
System.out.println("if lock1 ");
synchronized (Lock.obj2) {
System.out.println("if lock2 ");
}
}
}
}
else{
while(true) {
synchronized (Lock.obj2) {
System.out.println("else lock2 ");
synchronized (Lock.obj1) {
System.out.println("else lock1 ");
}
}
}
}
}
}
public class DeadLockDemo {
public static void main(String[] args){
Thread t1 = new Thread(new Dead(true));
Thread t2 = new Thread(new Dead(false));
t1.start();
t2.start();
}
}
java死锁案例
最新推荐文章于 2022-11-28 12:16:35 发布