I wonder whethe开发者_如何学Gor it is possible to detect deadlocks dynamically in Java by using the JVMTI. There are two events indicating actions on monitors using the synchronized statement:
Monitor Contended Enter
Sent when a thread is attempting to enter a Java programming language monitor already acquired by another thread.
Monitor Contended Entered
Sent when a thread enters a Java programming language monitor after waiting for it to be released by another thread.
This means, with JVMTI I can only see those monitors which are already locked. I wanted to reconstruct a wait-for graph, but without events indicating me, that a lock was acquired which is not held by any thread. This is impossible.
Are there alternatives? The SIGQUIT command on Unix allows a thread dump which displays the deadlocks, it seems like this is not possible within JVMTI.
You should be able to get this information via JMX.
Try
ManagementFactory.getThreadMXBean().findMonitorDeadlockedThreads();
精彩评论