public static void main(String[] args) { ThreadMXBean mbean = ManagementFactory.getThreadMXBean(); // 只能检测 synachrozied 同步代码块 的死锁 // long[] deadlockedThreadIds = mbean.findMonitorDeadlockedThreads(); // 可以检测 juc下的 Lock造成的死锁和 synachrozied代码块的死锁 long[] deadlockedThreadIds = mbean.findDeadlockedThreads(); if (deadlockedThreadIds != null) { ThreadInfo[] threadInfos = mbean.getThreadInfo(deadlockedThreadIds); for (ThreadInfo ti : threadInfos) { System.out.println(ti); } } }