Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 6 of 6
Once the deadlock occurs, hitting Ctrl-\ generates a profile file containing a Monitor Dump section, like so:
MONITOR DUMP BEGIN
THREAD 8, trace 2018, status: CW
THREAD 7, trace 2027, status: MW
THREAD 6, trace 2016, status: MW
THREAD 5, trace 2036, status: MW
THREAD 4, trace 2037, status: CW
THREAD 3, trace 2038, status: CW
THREAD 2, trace 2039, status: R
MONITOR java/lang/ref/Reference$Lock(80ddd80) unowned
waiting to be notified: thread 3
MONITOR SynchronizedObject(81ece98)
owner: thread 7, entry count: 1
waiting to enter: thread 6
MONITOR SynchronizedObject(81ecd40)
owner: thread 6, entry count: 1
waiting to enter: thread 7
MONITOR java/lang/ref/ReferenceQueue$Lock(80e7010) unowned
waiting to be notified: thread 4
...
We can see that threads 6 and 7 (LeftToRight and RightToLeft from the Thread section) are both in waiting states. Thread 7 holds one of the SynchronizedObject instance locks while thread 6 waits for that lock, and thread 6 holds the other lock while thread 7 waits for it. The traces
below confirm that each thread dutifully waits to obtain the lock held by the other thread -- exactly where we expect them
to wait:
TRACE 2027: (thread=7)
RightToLeftGrabber.grabRightToLeft(ClassicDeadlock.java:68)
RightToLeftGrabber.run(ClassicDeadlock.java:62)
TRACE 2016: (thread=6)
LeftToRightGrabber.grabLeftToRight(ClassicDeadlock.java:88)
LeftToRightGrabber.run(ClassicDeadlock.java:82)
hprof has shown us what stopped this program in its tracks.
By writing simple control experiment programs and running them under your chosen profiling tool, you can gain experience with the tool and confidence to use it when you venture into uncharted territory. The programs examined in this article model three common problems. You can use them as-is with other profilers and let them serve as a starting point for your own experimentation.
hprof may not be the easiest tool with which to work, but it's freely available to anyone using the Java 2 Platform and can effectively track down runtime problems like these.