Java Tools Tutorials - Herong's Tutorial Examples - v6.23, by Herong Yang
jstack - Detecting Java Thread Deadlocks
This section provides a tutorial example on how to detect Java thread deadlocks with the thread stack trace dump tool, 'jstack'.
With the deadlock demo program, SimpleDeadLock.java, running in a locked status as described in the previous section, I am ready to run "jstack" to print the deadlock information and stack traces of 2 locked threads:
herong> jps -l -m 16308 jdk.jcmd/sun.tools.jps.Jps -l -m 12888 SimpleDeadLock herong> jstack 12888 Full thread dump Java HotSpot(TM) 64-Bit Server VM (10.0.1+10 mixed mode): Threads class SMR info: _java_thread_list=0x0000000024138f00, length=13, elements={... } ... "Thread-0" #12 prio=5 os_prio=0 tid=0x000000002413e800 nid=0x30e0 waiting for monitor entry [0x0000000024a4e000] java.lang.Thread.State: BLOCKED (on object monitor) at SimpleDeadLock$Thread1.run(SimpleDeadLock.java:23) - waiting to lock <0x00000000c7a49f40> (a java.lang.Object) - locked <0x00000000c7a49f30> (a java.lang.Object) "Thread-1" #13 prio=5 os_prio=0 tid=0x0000000024141800 nid=0x1cc8 waiting for monitor entry [0x0000000023f4f000] java.lang.Thread.State: BLOCKED (on object monitor) at SimpleDeadLock$Thread2.run(SimpleDeadLock.java:36) - waiting to lock <0x00000000c7a49f30> (a java.lang.Object) - locked <0x00000000c7a49f40> (a java.lang.Object) ... "G1 Young RemSet Sampling" os_prio=2 tid=0x0000000022077800 nid=0x401c runnable "VM Periodic Task Thread" os_prio=2 tid=0x000000002413a000 nid=0x1c40 waiting on condition JNI global references: 2 Found one Java-level deadlock: ============================= "Thread-0": waiting to lock monitor 0x000000002334a280 (object 0x00000000c7a49f40, a java.lang.Object), which is held by "Thread-1" "Thread-1": waiting to lock monitor 0x000000002334a480 (object 0x00000000c7a49f30, a java.lang.Object), which is held by "Thread-0" Java stack information for the threads listed above: =================================================== "Thread-0": at SimpleDeadLock$Thread1.run(SimpleDeadLock.java:23) - waiting to lock <0x00000000c7a49f40> (a java.lang.Object) - locked <0x00000000c7a49f30> (a java.lang.Object) "Thread-1": at SimpleDeadLock$Thread2.run(SimpleDeadLock.java:36) - waiting to lock <0x00000000c7a49f30> (a java.lang.Object) - locked <0x00000000c7a49f40> (a java.lang.Object) Found 1 deadlock.
The output of "jstack" is very useful for debugging. It tells me:
Table of Contents
javac - The Java Program Compiler
java - The Java Program Launcher
jpackage - Binary Package Builder
javadoc - The Java Document Generator
jdeps - The Java Class Dependency Analyzer
jdeprscan - The Java Deprecated API Scanner
jcmd - The JVM Diagnostic Tool
jconsole - Java Monitoring and Management Console
jstat - JVM Statistics Monitoring Tool
JVM Troubleshooting Tools in JDK
jinfo - VM Option Value Checker
jinfo - Changing HotSpot VM Option
jstack - Stack Tracer to Generate Thread Dump
Java Thread Deadlock Demo Program
►jstack - Detecting Java Thread Deadlocks
Printing Histogram of Java Object Heap
jmap - Generating Heap Dump File
jhat - Java Heap Analysis Tool
jhat - Starting Web Server on a Heap Dump File
Listing Instance Counts of All Classes
Browsing Object Instance Values
Searching for Instances with OQL Statements
jhsdb - The Java HotSpot Debugger
jvisualvm (Java VisualVM) - JVM Visual Tool
javap - The Java Class File Disassembler
keytool - Public Key Certificate Tool
jrunscript - Script Code Shell
native2ascii - Native-to-ASCII Encoding Converter