Deadlock detection in Java

后端 未结 16 1078
野的像风
野的像风 2020-11-28 20:21

Long time ago, I saved a sentence from a Java reference book: \"Java has no mechanism to handle deadlock. it won\'t even know deadlock occurred.\" (Head First Java 2nd E

16条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-28 20:49

    If you are on Java 5 you can call the method findMonitorDeadlockedThreads() on the ThreadMXBean which you can get through a call of java.lang.management.ManagementFactory.getThreadMXBean(). This will find deadlocks caused by object monitors only. On Java 6 there's findDeadlockedThreads() which will also find deadlocks caused by "ownable synchronizers (for example ReentrandLock and ReentrantReadWriteLock).

    Be aware that it will probably be expensive to call these methods, so they should be used for troubleshooting purposes only.

提交回复
热议问题