Java中的程序性死锁检测 [英] Programmatic deadlock detection in java
本文介绍了Java中的程序性死锁检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何以编程方式检测Java程序中是否发生死锁
How can I programmatically detect that a deadlock has occurred in a Java program?
推荐答案
您可以使用JDK随附的 ThreadMXBean
以编程方式执行此操作:
You can do this programmatically using the ThreadMXBean
that ships with the JDK:
ThreadMXBean bean = ManagementFactory.getThreadMXBean();
long[] threadIds = bean.findDeadlockedThreads(); // Returns null if no threads are deadlocked.
if (threadIds != null) {
ThreadInfo[] infos = bean.getThreadInfo(threadIds);
for (ThreadInfo info : infos) {
StackTraceElement[] stack = info.getStackTrace();
// Log or store stack trace information.
}
}
显然你应该尝试隔离任何线程执行这个死锁检查 - 否则如果该线程死锁,它将无法运行检查!
Obviously you should try to isolate whichever thread is performing this deadlock check - Otherwise if that thread deadlocks it won't be able to run the check!
顺便说一下,这是JConsole在封底下使用。
Incidentally this is what JConsole is using under the covers.
这篇关于Java中的程序性死锁检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文