package com.myspringboot.test;
import java.util.concurrent.TimeUnit;
/*
* @Description:死锁案例
* @Author: WDC
* @Date: 2020/3/19 10:50
**/
class DeathLockDemo implements Runnable{
private String lockA;
private String lockB;
public DeathLockDemo(String lockA, String lockB) {
this.lockA = lockA;
this.lockB = lockB;
}
@Override
public void run() {
synchronized (lockA){
System.out.println(Thread.currentThread().getName()+"\t自己持有"+lockA+"\t并尝试获得"+lockB);
try { TimeUnit.SECONDS.sleep(2); } catch (InterruptedException e) { e.printStackTrace(); }
synchronized (lockB){
System.out.println(Thread.currentThread().getName()+"\t自己持有"+lockB+"\t并尝试获得"+lockA);
}
}
}
}
public class DeathLock{
public static void main(String[] args) {
String lockA = "AAA";
String lockB = "BBB";
new Thread(new DeathLockDemo(lockA,lockB),"ThreadAAA").start();
new Thread(new DeathLockDemo(lockB,lockA),"ThreadBBB").start();
}
}
死锁案例
最新推荐文章于 2020-09-19 13:11:51 发布