INDEX
制造一个死锁
public class DeadlockDemo {
public static void work(String lock1,String lock2){
synchronized (lock1){
System.out.println(Thread.currentThread().getName()+" has "+ lock1 + " wait for " + lock2);
try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); }
synchronized (lock2){
System.out.println(" had "+ lock2);
}
}
}
public static void main(String[] args) {
new Thread(()->{
work("1","2");
},"AAA").start();
new Thread(()->{
work("2","1");
},"BBB").start();
}
}
现象
排查
获取有问题的 java 进程的编号 jps -l
查看进程的栈信息 jstack 7976
也可以使用 jconsole
的排查死锁功能
idea 工具配置
需要配置环境变量和 path