package t1;
// 死锁
public class T3 {
public static void main(String[] args) {
String lockA = "a";
String lockB = "b";
new Thread(new HoldLockThread(lockA,lockB)).start();
new Thread(new HoldLockThread(lockB,lockA)).start();
}
}
class HoldLockThread implements Runnable{
private String locka;
private String lockb;
public HoldLockThread(String locka, String lockb) {
this.locka = locka;
this.lockb = lockb;
}
@Override
public void run() {
synchronized (locka){
System.out.println(Thread.currentThread().getName() + " 自己持有" + locka + "尝试获得" + lockb);
try {
Thread.sleep(2000);
} catch (Exception e){
e.printStackTrace();
}
synchronized (lockb){
System.out.println(Thread.currentThread().getName() + " 自己持有" + lockb + "尝试获得" + locka);
}
}
}
}
死锁的代码
最新推荐文章于 2023-06-08 19:38:27 发布