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

 About This Book

 Java Tools Terminology

 Java Tools Included in JDK

 javac - The Java Program Compiler

 java - The Java Program Launcher

 jar - The JAR File Tool

 jlink - The JRE Linker

 jmod - The JMOD File Tool

 jimage - The JIMAGE File Tool

 jpackage - Binary Package Builder

 javadoc - The Java Document Generator

 jdeps - The Java Class Dependency Analyzer

 jdeprscan - The Java Deprecated API Scanner

 jdb - The Java Debugger

 jcmd - The JVM Diagnostic Tool

 jconsole - Java Monitoring and Management Console

 jstat - JVM Statistics Monitoring Tool

JVM Troubleshooting Tools

 JVM Troubleshooting Tools in JDK

 jinfo - VM Option Value Checker

 jinfo - Changing HotSpot VM Option

 jstack - Stack Tracer to Generate Thread Dump

 What Is a Thread Dump?

 Java Thread Deadlock Demo Program

jstack - Detecting Java Thread Deadlocks

 jmap - JVM Heap Dump Tool

 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

 Object Query Language (OQL)

 Searching for Instances with OQL Statements

 jhsdb - The Java HotSpot Debugger

 jvisualvm (Java VisualVM) - JVM Visual Tool

 jmc - Java Mission Control

 javap - The Java Class File Disassembler

 keytool - Public Key Certificate Tool

 jarsigner - JAR File Signer

 jshell - Java Language Shell

 jrunscript - Script Code Shell

 Miscellaneous Tools

 native2ascii - Native-to-ASCII Encoding Converter

 JAB (Java Access Bridge) for Windows

 Archived Tutorials

 References

 Full Version in PDF/EPUB