博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
多线程死锁示例
阅读量:5735 次
发布时间:2019-06-18

本文共 741 字,大约阅读时间需要 2 分钟。

class Test implements Runnable{

private boolean flag;
Test(boolean flag){
this.flag = flag;
}
public void run(){
if(flag){
synchronized(MyLock.lockA){
System.out.println("if lockA");
synchronized(MyLock.lockB){
System.out.println("if lockB");
}
}
}else{
synchronized(MyLock.lockB){
System.out.println("else lockB");
synchronized(MyLock.lockA){
System.out.println("else lockA");
}
}
}
}
}

class MyLock{

public static final MyLock lockA= new MyLock();
public static final MyLock lockB = new MyLock();
}

class DemoLockTest{

public static void main(String[] arg){
Test a = new Test(true);
Test b = new Test(false);
Thread t1 = new Thread(a);
Thread t2 = new Thread(b);
t1.start();
t2.start();
}

}

转载于:https://www.cnblogs.com/chzlh/p/9265613.html

你可能感兴趣的文章