package day13;
/**
* @author QITM
* @date 2020/4/21 8:22
* @描述
*/
class Test implements Runnable{
private boolean flag;
Test(Boolean flag) {
this.flag=flag;
}
@Override
public void run(){
if(flag){
synchronized (MyLock.locka){
System.out.println(Thread.currentThread().getName()+ "if locka ....");
synchronized (MyLock.lockb){
System.out.println(Thread.currentThread().getName()+"if lockb ....");
}
}
}else {
synchronized (MyLock.lockb){
System.out.println(Thread.currentThread().getName()+"else locka");
synchronized (MyLock.locka){
System.out.println(Thread.currentThread().getName()+"else lockb");
}
}
}
}
}
class MyLock{
public static final Object locka = new Object();
public static final Object lockb = new Object();
}
public class DeadLockTest {
public static void main(String[] args) {
Test a = new Test(true);
Test b = new Test(false);
Thread thread = new Thread(a);
Thread thread1 = new Thread(b);
thread.start();
thread1.start();
}
}
java中书写一个简单的死锁demo
最新推荐文章于 2023-01-14 04:02:30 发布