How to Take a Java Thread Dump: Every Method and Option
jcmd Thread.print, jstack, kill -3, Ctrl+Break, ThreadMXBean, Actuator and Kubernetes, the JSON dump for virtual threads, and why you take three.
By Redbison Dev · · 11 min read

Requests are timing out, CPU is flat, and nobody can say what the JVM is doing. A thread dump answers that in milliseconds without stopping the service: run jcmd <pid> Thread.print -l > dump-1.txt, wait five seconds, and do it twice more. Where only the console log is reachable, kill -3 <pid> prints the same dump to the JVM's standard output. Every command below was run against OpenJDK 17.0.21.1 and the options shown are JDK 17's unless another version is named; the three dumps in the worked example are real, and the Java Thread Dump Analyzer reads them in your browser.
Find the process
jcmd -l (or jps -l) lists the JVMs you can attach to. The jcmd manual requires the same machine and the same effective user and group as the target, so run it as the user that started the JVM; in a container whose entrypoint is java, the pid is usually 1. A main class name works instead of a pid, but the manual says it targets every JVM whose main class matches.
jcmd Thread.print: the default choice
$ jcmd 55336 help Thread.print
Impact: Medium: Depends on the number of threads.
Syntax : Thread.print [options]
-l : [optional] print java.util.concurrent locks (BOOLEAN, false)
-e : [optional] print extended thread information (BOOLEAN, false)
Oracle's JDK 17 jcmd page lists only -l; the JDK 21 page lists both, and the 17.0.21 VM accepts both, so the binary's own help is the reference this guide follows. What each adds, from diffing dumps of the same process:
- No option: each thread's header line (name,
#id, daemon,prio,cpu=,elapsed=,tid,nid), itsjava.lang.Thread.State, and frames annotated with- locked,- waiting to lockand- waiting onfor monitors. -l: aLocked ownable synchronizers:block per thread, which is whereReentrantLockand otherjava.util.concurrentlocks appear. Without it, a deadlock onReentrantLockis invisible, so always pass-l.-e: two more header fields,allocated=1864M defined_classes=32in the test, bytes allocated and classes defined by that thread.
A real header and the lock lines that matter, from the worked example's first dump:
"order-worker-15" #15 prio=5 os_prio=31 cpu=0.14ms elapsed=8.21s tid=0x0000000948d80c00 nid=0xa903 waiting for monitor entry [0x000000016e322000]
java.lang.Thread.State: BLOCKED (on object monitor)
at OrderService.lambda$main$2(OrderService.java:25)
- waiting to lock <0x00000005760002e0> (a java.lang.Object)
Locked ownable synchronizers:
- <0x0000000576000548> (a java.util.concurrent.ThreadPoolExecutor$Worker)
cpu= is CPU time since the thread started and elapsed= its wall time alive; nid is the operating system thread id in hex, which is how you match a thread to a hot row in top -H. jcmd writes to its own stdout, so the redirect saves the file on your side; the first line of the file is the pid followed by a colon.
jstack, and why it is second
jstack -l 55336 > dump.txt
The 17 binary's usage is jstack [-l][-e] <pid>, the same two options with the same output; the only difference in testing was that jstack omits the <pid>: first line. The JDK 17 jstack manual says "This command is experimental and unsupported" and "might not be available in future releases of the JDK", which is reason enough to write runbooks with jcmd. JDK 8's jstack also had -F to force a dump from a JVM that does not respond and -m for mixed Java and native frames; JDK 17 rejects -F with Use jhsdb jstack instead, and jhsdb jstack --pid <pid> takes --locks and --mixed.
kill -3, SIGQUIT and Ctrl+Break
kill -3 <pid> (SIGQUIT) asks the JVM to print a thread dump on its own standard output. The process keeps running: in testing it carried on serving after the signal, and the dump appeared in its stdout log, followed by a Heap section with the G1 heap and Metaspace usage, which jcmd does not print. That destination is the point. Under systemd it goes to the journal, under Docker to docker logs, under Kubernetes to kubectl logs, so it works in an image with no JDK tools at all. The price is that the dump is mixed into application logs and has to be cut out.
On Windows the same dump comes from Ctrl+Break in the console window running the JVM. Both depend on the JVM's signal handling: the java manual says that with -Xrs, SIGQUIT thread dumps (and Ctrl+Break on Windows) are not available, so check the command line with jcmd <pid> VM.command_line if nothing appears. Never use kill -9 or plain kill for this; those end the process.
The new dump for virtual threads: Thread.dump_to_file
Traditional dumps are a flat list of platform threads. JEP 444, which made virtual threads final in JDK 21, says outright that "we will not extend traditional thread dumps to include virtual threads" and introduces a new jcmd dump instead:
jcmd <pid> Thread.dump_to_file -format=json /tmp/threads.json
The JEP adds that this format "does not include object addresses, locks, JNI statistics, heap statistics", and that generating it "does not pause the application". The JDK 22 jcmd manual documents its two options, -format (plain or json, default plain) and -overwrite; the JDK 24 manual adds %p in the path expanding to the pid. The JDK 21 manual page omits the command even though JEP 444 shows it, and on 17.0.21 it fails with java.lang.IllegalArgumentException: Unknown diagnostic command. Use it on JDK 21 and later when virtual threads carry the work, and keep Thread.print -l for lock analysis, since only the traditional dump shows monitors. Format the JSON with the JSON Formatter to read it.
From inside the application
ThreadMXBean tm = ManagementFactory.getThreadMXBean();
for (ThreadInfo ti : tm.dumpAllThreads(true, true)) { // locked monitors, locked synchronizers
StackTraceElement[] frames = ti.getStackTrace();
}
One trap measured on 17: ThreadInfo.toString() printed 8 frames and then ... for a thread whose getStackTrace() returned 25. The javadoc only says the string's format "depends on the implementation", so build your own output from getStackTrace(), getLockInfo() and getLockedSynchronizers(). The same bean is in any JMX console, and VisualVM's Threads tab takes a dump of a local application without stopping it.
Spring Boot Actuator's /actuator/threaddump returns JSON with Accept: application/json and jstack-style text with Accept: text/plain:
curl -H 'Accept: text/plain' http://localhost:8081/actuator/threaddump > dump.txt
The Spring Boot reference says only health is exposed over HTTP by default, so the endpoint must be added to management.endpoints.web.exposure.include, ideally on a management port that is not public. A thread dump leaks far less than a heap dump, but it still names every internal class and package, and thread names can carry host names.
Kubernetes and Docker
jcmd's output travels back over the exec stream, so no copy step is needed:
for i in 1 2 3; do
kubectl exec -n shop orders-7d9f -c app -- jcmd 1 Thread.print -l > dump-$i.txt
sleep 5
done
docker exec <container> jcmd 1 Thread.print -l > dump.txt is the Docker equivalent. Both need jcmd in the image (a JRE-only image lacks it) and the exec user must match the JVM's user. Without either, kubectl exec <pod> -- kill -3 1 and then kubectl logs <pod> gets the dump through stdout, provided the image has a kill binary. A process that has already been restarted has nothing left to dump, so capture before acting on a failing liveness probe.
Take three, five to ten seconds apart
One dump shows where every thread is; it cannot show whether a thread is stuck or just passing through. Three dumps can. Our demo service holds a lock in one thread forever while four pool workers queue on it; three jcmd Thread.print -l dumps taken five seconds apart read, in the analyzer:
| Dump | Taken | main cpu | order-worker-15 cpu | order-worker-15 state | Lock waiters |
|---|---|---|---|---|---|
| 1 | 00:29:44 | 96.82 ms | 0.14 ms | BLOCKED | 4, held by ledger-writer |
| 2 | 00:29:49 | 127.81 ms | 0.14 ms | BLOCKED | 4, held by ledger-writer |
| 3 | 00:29:54 | 162.79 ms | 0.14 ms | BLOCKED | 4, held by ledger-writer |
The workers' CPU did not move in ten seconds and they wait on the same monitor every time: stuck, not slow. main gained about 31 and 35 ms per interval: alive. Each dump reports 17 Java threads (12 daemon) plus 15 VM threads, 9 RUNNABLE, 4 BLOCKED, 3 TIMED_WAITING and 1 WAITING. Five to ten seconds is long enough for a healthy thread to move on and short enough to catch the same incident; paste two dumps into the Online Diff Tool to see which stacks moved.
What a dump costs
From the JVM's safepoint log (-Xlog:safepoint) during the tests: Safepoint "PrintThreads" held the 35-thread demo service for 0.186 ms, and the same command on a JVM with a 2.3 GB heap for 0.75 ms. The pause scales with the number of threads and stack depth, not the heap, which is why thread dumps come before heap dumps: a heap dump of that 2.3 GB JVM froze it for 4.79 seconds. jcmd also runs a deadlock check as a separate FindDeadlocks safepoint of a few microseconds, and prints a "Found one Java-level deadlock" section when it finds one.
Which method for which situation
| Situation | Use |
|---|---|
| Shell on the host, JDK 9 or later | jcmd <pid> Thread.print -l |
| Script that must also run on JDK 8 | jstack -l <pid> |
| Only the log is reachable, or no JDK tools in the image | kill -3 <pid>, read stdout |
| Windows console | Ctrl+Break |
| JDK 21+ with virtual threads | jcmd <pid> Thread.dump_to_file -format=json <file> |
| Dump from code or a watchdog | ThreadMXBean.dumpAllThreads(true, true) |
| Spring Boot with Actuator on a private port | /actuator/threaddump with Accept: text/plain |
| Kubernetes | kubectl exec ... jcmd 1 Thread.print -l > dump.txt, three times |
| JDK 8 JVM that ignores attach | jstack -F <pid>; on 17, jhsdb jstack --pid <pid> |
If the problem is memory rather than a hang, follow with a Java heap dump, accepting its pause, and check the GC log first: a process that spends its time in full GCs looks stuck in every thread dump.
Tools for this job
- Java Thread Dump Analyzer: paste jcmd, jstack or SIGQUIT output; deadlocks, lock contention, pools and CPU, verdict first.
- Online Diff Tool: diff two dumps taken seconds apart; stacks that did not change are the stuck ones.
- JSON Formatter: make a
Thread.dump_to_file -format=jsondump readable and queryable.
To read the states and lock lines in depth, the heap and thread dump analysis guide covers BLOCKED versus WAITING and the Object.wait trap, and the GC log reading guide covers the collector side of a slow JVM.
Frequently asked questions
Does a thread dump pause or slow the JVM?
Only briefly. It runs at a safepoint, so every Java thread stops while the stacks are walked: 0.186 ms for a 35-thread service and 0.75 ms on a larger JVM in testing. Thousands of threads with deep stacks take longer, but it stays far below a heap dump.
Where does kill -3 output go?
To the JVM's standard output, not to your terminal. Look in the console, the systemd journal, docker logs or kubectl logs, wherever that process's stdout is collected. The JVM keeps running. If nothing appears, check whether it was started with -Xrs, which disables SIGQUIT dumps.
Why do jstack and jcmd show no virtual threads?
The traditional thread dump lists platform threads only, by design in JEP 444. On JDK 21 and later use jcmd Thread.dump_to_file, in plain text or JSON, which lists virtual threads alongside platform threads but omits lock details. Take both when lock contention is the question.
Why do I get "Unable to open socket file" from jcmd?
On Linux, jcmd attaches through a socket the target JVM creates as /tmp/.java_pid followed by its pid, and the JDK 17 attach code reports that error when the socket never answers. The usual cause is a different user: the manual requires the JVM's effective user and group. Run jcmd as that user, or fall back to kill -3.
Sources and further reading
- The jcmd Command (JDK 21), Oracle. Thread.print with -e and -l, and the same-user rule.
- The jcmd Command (JDK 25), Oracle. Thread.dump_to_file options and %p expansion.
- The jstack Command (JDK 17), Oracle. Its experimental and unsupported status.
- JEP 444: Virtual Threads, OpenJDK. The new thread dump format and why traditional dumps omit virtual threads.
- The java Command (JDK 17), Oracle. -Xrs and what it disables.
- ThreadInfo (JDK 17), Oracle. Stack traces and lock information from ThreadMXBean.
- Thread Dump endpoint, Spring. JSON and text formats.