0

I wonder whether 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.

Konrad Reiche
  • 27,743
  • 15
  • 106
  • 143

1 Answers1

1

You should be able to get this information via JMX.

Try

ManagementFactory.getThreadMXBean().findMonitorDeadlockedThreads();
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Yes, but this would be using a java agent, not a native agent. – Konrad Reiche Jun 02 '11 at 15:35
  • You can call Java code from native code. I don't see what the problem is. – Peter Lawrey Jun 02 '11 at 15:37
  • I didn't think native agents used byte code, I thought they were written in C. – Peter Lawrey Jun 02 '11 at 15:44
  • They are, so can I use the JNI to make calls to Java methods like descriped here? http://journals.ecs.soton.ac.uk/java/tutorial/native1.1/implementing/method.html – Konrad Reiche Jun 02 '11 at 15:48
  • Correct, anything you can do in a Java agent, you can do in a native agent and visa versa with JNI. ;) On bizarre example is File.length() which is a native method, but the first thing it does is convert the character encoding of the file name in Java via JNI. :P – Peter Lawrey Jun 02 '11 at 15:56