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
JDK 5 and 6 will dump held lock information in a full thread dump (obtained with kill -3, jstack, jconsole, etc). JDK 6 even contains information about ReentrantLock and ReentrantReadWriteLock. It is possible from this information to diagnose a deadlock by finding a lock cycle: Thread A holds lock 1, Thread B holds lock 2, and either A is requesting 2 or B is requesting 1. From my experience, this is usually pretty obvious.
Other analysis tools can actually find potential deadlocks even if they don't occur. Thread tools from vendors like OptimizeIt, JProbe, Coverity, etc are good places to look.
After so long, i am able to write the simplest example of Deadlock. Comments are welcome.
Class A
{
synchronized void methodA(B b)
{
b.last();
}
synchronized void last()
{
SOP(“ Inside A.last()”);
}
}
Class B
{
synchronized void methodB(A a)
{
a.last();
}
synchronized void last()
{
SOP(“ Inside B.last()”);
}
}
Class Deadlock implements Runnable
{
A a = new A();
B b = new B();
// Constructor
Deadlock()
{
Thread t = new Thread();
t.start();
a.methodA(b);
}
public void run()
{
b.methodB(a);
}
public static void main(String args[] )
{
new Deadlock();
}
}
Since JDK 1.5 there are very useful methods in the java.lang.management
package to find and inspect deadlocks that occurs. See the findMonitorDeadlockedThreads()
and findDeadlockedThreads()
method of the ThreadMXBean
class.
A possible way to use this is to have a separate watchdog thread (or periodic task) that does this.
Sample code:
ThreadMXBean tmx = ManagementFactory.getThreadMXBean();
long[] ids = tmx.findDeadlockedThreads();
if (ids != null) {
ThreadInfo[] infos = tmx.getThreadInfo(ids, true, true);
System.out.println("The following threads are deadlocked:");
for (ThreadInfo ti : infos) {
System.out.println(ti);
}
}
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.
Dr. Heinz Kabutz of JavaSpecialists has written an entertaining and informative newsletter issue on Java deadlocks and describes something called a ThreadMXBean in another newsletter issue. Between those, you should get a good idea of the issues and some pointers to doing your own instrumentation.
If you're debugging in eclipse, you can pause the application (select the app in the debug view and the little || button on the debug toolbar) and then it can report deadlocks.
See http://runnerwhocodes.blogspot.com/2007/10/deadlock-detection-with-eclipse.html for an example.