import lombok.SneakyThrows;
import java.util.concurrent.*;
public class ThreadDemo {
public static void main(String[] args) {
String lockA="A";
String lockB="B";
new Thread(()->{
new DeadLock(lockA,lockB).run();
}).start();
new Thread(()->{
new DeadLock(lockB,lockA).run();
}).start();
}
}
class DeadLock implements Runnable{
String lockA;
String lockB;
public DeadLock(String lockA, String lockB) {
this.lockA = lockA;
this.lockB = lockB;
}
@SneakyThrows
@Override
public void run() {
synchronized (lockA){
System.out.println("A----"+lockB);
TimeUnit.SECONDS.sleep(300L);
synchronized (lockB){
System.out.println("B-----"+lockA);
}
}
}
}
死锁Demo
最新推荐文章于 2021-12-15 14:31:29 发布