package test.itheima;
public class DeadLockerTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Thread t1 = new Thread(new Test(true));
Thread t2 = new Thread(new Test(false));
t1.start();
t2.start();
}
}
class Test implements Runnable {
private boolean flag;
Test(boolean flag) {
this.flag = flag;
}
public void run() {
if (flag) {
while (true) {
synchronized (MyLocler.lockerA) {
System.out.println("if MyLocler.lockerA");
synchronized (MyLocler.lockerB) {
System.out.println("if MyLocler.lockerB");
}
}
}
} else {
while (true) {
synchronized (MyLocler.lockerB) {
System.out.println("else MyLocler.lockerB");
synchronized (MyLocler.lockerA) {
System.out.println("else MyLocler.lockerA");
}
}
}
}
}
}
class MyLocler {
static Object lockerA = new Object();
static Object lockerB = new Object();
}
[黑马程序员]多线程__蛋疼的死锁
最新推荐文章于 2023-05-02 16:37:16 发布