死锁检测工具 发表于 2020-09-17 | 分类于 JVM | 本文总阅读量 次 可视化检测工具“cmd” -> jvisualvm,打开 “Java VisualVM” 命令行检测工具12# 找到我们的程序idjps -l 12# 查看线程情况jstack 9896 死锁案例代码12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455public class DeadLockTest { private Object object1 = new Object(); private Object object2 = new Object(); public static void main(String[] args) { final DeadLockTest deadLockTest = new DeadLockTest(); Thread thread01 = new Thread(new Runnable() { @Override public void run() { while (true) { deadLockTest.method01(); try { Thread.sleep(100); } catch (InterruptedException e) { } } } }, "thread01"); Thread thread02 = new Thread(new Runnable() { @Override public void run() { while (true) { deadLockTest.method02(); try { Thread.sleep(100); } catch (InterruptedException e) { } } } }, "thread02"); thread01.start(); thread02.start(); } public void method01() { synchronized (object1) { synchronized (object2) { System.out.println("method01 invoked"); } } } public void method02() { synchronized (object2) { synchronized (object1) { System.out.println("method02 invoked"); } } }}