How to take a thread dump
jstack -l <pid>- The classic.
-ladds the java.util.concurrent locks each thread holds ("ownable synchronizers"), without which a ReentrantLock deadlock cannot be seen. Find the pid withjpsorjcmd. jcmd <pid> Thread.print -l- The same output as jstack, from the tool the JDK now recommends.
jcmd <pid> Thread.dump_to_file -format=json out.json- Java 21 and later. The only format that includes virtual threads, which jstack leaves out; it also groups threads by the executor or scope that owns them.
kill -3 <pid>- On Linux and macOS, makes the JVM print a dump to its own standard output (the application's log), without any JDK tools on the machine. On Windows, Ctrl+Break in the console.
Take three dumps a few seconds apart: a thread in the same place in all three is stuck; one that moves is just busy.
Thread states
| RUNNABLE | Running Java code, or in native code (including blocking I/O: a thread reading a socket is RUNNABLE). |
|---|---|
| BLOCKED | Waiting to enter a synchronized block or method whose monitor another thread holds. |
| WAITING | Waiting with no time limit: Object.wait(), Thread.join(), or parked by LockSupport (j.u.c locks, queues, latches). |
| TIMED_WAITING | Waiting with a time limit: Thread.sleep(), wait(ms), parkNanos, a poll with a timeout. |
| NEW | Created but not started. |
| TERMINATED | Finished. |
RUNNABLE does not mean using CPU: a thread blocked reading a socket is RUNNABLE, because the JVM cannot tell native I/O from work. Look at the top frame (socketRead0, epollWait, accept) to tell them apart.
Reading the locks
- locked <0x…>- This frame holds the monitor: it is inside
synchronizedon that object. - waiting to lock <0x…>- BLOCKED, trying to enter
synchronizedon an object another thread holds. Search the dump for the same address to find the holder. - waiting on <0x…>- In
Object.wait(): it gave the monitor up and waits fornotify(). The "locked" line below it is stale; the thread does not hold the lock now. - parking to wait for <0x…>- Parked by a java.util.concurrent lock, condition, queue or latch. For a lock, the holder appears under "Locked ownable synchronizers" (with
-l).
A deadlock is a cycle: thread A holds X and waits for Y, thread B holds Y and waits for X. HotSpot detects monitor and ownable-synchronizer deadlocks itself and prints "Found one Java-level deadlock" at the end of the dump; this page finds the same cycles in every format, including JSON dumps, where the JVM does not report them.