1、死锁案例
/**
* @descpription: 死锁案例
* @date 2024/6/17
*/
public class DeadLockDemo {
public static void main(String[] args) {
Object objA = new Object();
Object objB = new Object();
new Thread(() ->{
synchronized (objA){
System.out.println(Thread.currentThread().getName() + "\t 持有A锁,希望获取B锁");
try {TimeUnit.MILLISECONDS.sleep(100);} catch (InterruptedException e) {throw new RuntimeException(e);}
synchronized (objB){
System.out.println(Thread.currentThread().getName() + "\t 成功持有B锁");
}
}
},"t1").start();
new Thread(() ->{
synchronized (objB){
System.out.println(Thread.currentThread().getName() + "\t 持有B锁,希望获取A锁");
try {TimeUnit.MILLISECONDS.sleep(100);} catch (InterruptedException e) {throw new RuntimeException(e);}
synchronized (objA){
System.out.println(Thread.currentThread().getName() + "\t 成功持有A锁");
}
}
},"t2").start();
}
}
2、运行效果
3、排查死锁
查看线程号
通过jstack查找死锁