1,死锁代码
package com.bandit.audition.concurrent;
public class DeadLockDemo {
private static String A = "A";
private static String B = "B";
public static void main(String[] args) {
new DeadLockDemo().deadLock();
}
private void deadLock(){
new Thread(() -> {
// 1,获取A对象锁
synchronized (A) {
try {
// 2,睡眠2s
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 5,此线程尝试获取B对象锁,失败
synchronized (B){
System.out.println(1);
}
}
}).start();
new Thread(() -> {
// 3,获取B对象锁
synchronized (B) {
try {
// 4,睡眠2s
Thread.sleep(2000);