Java Virtual Thread Dumps With jcmd: JSON Format, Thread Containers and HotSpotDiagnosticMXBean
Why the classic thread dump misses virtual threads, and how jcmd Thread.dump_to_file and HotSpotDiagnosticMXBean.dumpThreads capture all of them in plain text or JSON. The thread-container tree, what each entry contains on Java 25, and a program that analyzes its own JSON dump.
kvi5hnuCode Sprout team member8 min readOn this page
A thread dump is a snapshot of every thread's stack: the first thing to take when an application hangs or slows down. With virtual threads, there's a catch: the classic dump commands only show platform threads. A server with 50,000 virtual threads waiting on a slow database would show a handful of idle carriers and nothing else.
The JDK has a newer dump that includes virtual threads, in two formats, and organizes them into thread containers.
Two kinds of thread dump
| Command | Includes virtual threads? | Notes |
|---|---|---|
jstack <pid>, jcmd <pid> Thread.print, Ctrl+\ (Ctrl+Break on Windows) | no | the traditional HotSpot format, with lock and deadlock information for platform threads |
jcmd <pid> Thread.dump_to_file <file> | yes | plain text (default) or -format=json, written to a file |
HotSpotDiagnosticMXBean.dumpThreads(file, format) | yes | the same dump from Java code or over JMX |
On a test with five sleeping virtual threads, jcmd Thread.print listed none of them, and Thread.dump_to_file listed all five. Use the new dump whenever virtual threads are involved. The new dump is written to a file instead of the console because it can be very large: a million virtual threads produce a million stacks.
jcmd 4242 Thread.dump_to_file /tmp/threads.txt # plain text
jcmd 4242 Thread.dump_to_file -format=json /tmp/threads.json # JSON
The command refuses to replace an existing file unless you add -overwrite.
Reading the plain-text dump
The plain format starts with the process ID, the time and the Java version, then lists every thread. A virtual thread is marked virtual, and on Java 25 each entry shows its state and the locks it holds:
#337 "quote-refresher" virtual TIMED_WAITING 2026-11-18T14:11:19.702259Z
at java.base/java.lang.VirtualThread.parkNanos(VirtualThread.java:787)
at java.base/java.lang.VirtualThread.sleepNanos(VirtualThread.java:983)
at java.base/java.lang.Thread.sleepNanos(Thread.java:507)
at java.base/java.lang.Thread.sleep(Thread.java:540)
at Solution.waitFor(Solution.java:18)
at Solution.lambda$main$1(Solution.java:39)
- locked <java.lang.Object@6659c656>
at java.base/java.lang.VirtualThread.run(VirtualThread.java:460)
#26 "" virtual TIMED_WAITING 2026-11-18T14:11:19.702691Z
at java.base/java.lang.VirtualThread.parkNanos(VirtualThread.java:787)
...
at Solution.lambda$main$0(Solution.java:36)
at java.base/java.util.concurrent.ThreadPerTaskExecutor$ThreadBoundFuture.run(ThreadPerTaskExecutor.java:323)
Two practical points stand out. First, virtual threads have empty names by default (""), so a dump of 10,000 unnamed threads is hard to read. Name virtual threads that do distinct jobs, using Thread.ofVirtual().name("quote-", 0) or a named factory, so dumps and logs say what each one is. Second, the frames from your own code tell you what each thread is waiting for, which is usually all you need.
The JSON dump and thread containers
The JSON format organizes threads into a tree of thread containers. A container is whatever created and owns a group of threads:
<root>holds platform threads such asmainand system threads, plus virtual threads started directly with theThreadAPI.- Each
ExecutorService, including eachnewVirtualThreadPerTaskExecutor(), is a container with its own threads. - The virtual-thread scheduler's
ForkJoinPoolis a container holding the carrier threads. - Each structured-concurrency
StructuredTaskScope(Part 4) is a container whose owner is the thread that opened it, with its subtasks inside. Nested scopes form nested containers, so the dump shows the task tree.

A trimmed entry from a Java 25 JSON dump looks like this:
{
"container": "java.util.concurrent.ThreadPerTaskExecutor@7291c18f",
"parent": "<root>",
"owner": null,
"threads": [
{
"tid": "26",
"time": "2026-11-18T14:10:21.424441Z",
"virtual": true,
"name": "",
"state": "TIMED_WAITING",
"stack": [
"java.base/java.lang.Thread.sleep(Thread.java:540)",
"Solution.waitFor(Solution.java:18)"
]
}
],
"threadCount": "300"
}
Java 21's JSON dump contained only tid, name and stack for each thread. Java 25 adds state, virtual, and lock details: monitorsOwned (the monitors a thread holds, with the frame depth), waitingOn (the monitor it's waiting on in Object.wait) and parkBlocker (the object it's parked on, such as a ReentrantLock or a ForkJoinPool). The JSON format is meant for tools. A script can count threads by state, group identical stacks, or find which thread holds a lock that hundreds are waiting for.
Taking and analyzing a dump from code
com.sun.management.HotSpotDiagnosticMXBean.dumpThreads produces the same dump from inside the application, which is useful for a diagnostics endpoint or a watchdog that dumps threads when a request takes too long. The output path must be absolute and the file must not exist yet.
This program starts 300 virtual threads in an executor and one named virtual thread that holds a monitor, dumps all threads as JSON, and analyzes the result. It uses plain string matching to stay dependency-free. Real tools would use a JSON library:
import com.sun.management.HotSpotDiagnosticMXBean;
import com.sun.management.HotSpotDiagnosticMXBean.ThreadDumpFormat;
import java.lang.management.ManagementFactory;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Solution {
static final Object quoteLock = new Object();
static void waitFor(long ms) {
try { Thread.sleep(ms); } catch (InterruptedException e) { Thread.currentThread().interrupt(); }
}
/** Writes a JSON thread dump; the path must be absolute and the file must not exist. */
static String jsonThreadDump() throws Exception {
Path file = Files.createTempDirectory("dump").resolve("threads.json").toAbsolutePath();
ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class)
.dumpThreads(file.toString(), ThreadDumpFormat.JSON);
String json = Files.readString(file);
Files.delete(file);
Files.delete(file.getParent());
return json;
}
public static void main(String[] args) throws Exception {
CountDownLatch ready = new CountDownLatch(301);
try (ExecutorService requests = Executors.newVirtualThreadPerTaskExecutor()) {
for (int i = 0; i < 300; i++) {
requests.submit(() -> { ready.countDown(); waitFor(2_000); }); // like slow partner calls
}
Thread.ofVirtual().name("quote-refresher").start(() -> {
synchronized (quoteLock) { ready.countDown(); waitFor(2_000); } // holds a monitor
});
ready.await();
Thread.sleep(200);
String json = jsonThreadDump();
// Each thread container in the dump, and how many virtual threads it holds
Map<String, Integer> virtualPerContainer = new TreeMap<>();
for (String block : json.split("\"container\": ")) {
if (!block.startsWith("\"")) continue;
String name = block.substring(1, block.indexOf('"', 1)).replaceAll("@\\p{XDigit}+", "");
int count = block.split("\"virtual\": true", -1).length - 1;
if (count > 0) virtualPerContainer.merge(name, count, Integer::sum);
}
System.out.println("virtual threads per container: " + virtualPerContainer);
// The states of all virtual threads
Matcher m = Pattern.compile("\"virtual\": true,\\s*\"name\": \"[^\"]*\",\\s*\"state\": \"(\\w+)\"").matcher(json);
Map<String, Integer> states = new TreeMap<>();
while (m.find()) states.merge(m.group(1), 1, Integer::sum);
System.out.println("virtual thread states: " + states);
// The named thread, and the lock it owns
int at = json.indexOf("\"name\": \"quote-refresher\"");
String entry = json.substring(at, json.indexOf("\"tid\"", at) > 0 ? json.indexOf("\"tid\"", at) : json.length());
System.out.println("quote-refresher owns a monitor: " + entry.contains("\"monitorsOwned\""));
}
}
}
Output on Java 25:
virtual threads per container: {<root>=1, java.util.concurrent.ThreadPerTaskExecutor=300}
virtual thread states: {TIMED_WAITING=301}
quote-refresher owns a monitor: true
The 300 executor threads are grouped in their executor's container, and the directly started thread is in the root. All are in TIMED_WAITING because they're sleeping. The dump also shows that quote-refresher holds a monitor, which is exactly the information you need when other threads are stuck waiting for it.
Using dumps to diagnose problems
- Hangs. Take two or three dumps a few seconds apart. Threads whose stacks don't change are stuck. Look at the frames from your code to see what they're waiting for.
- Lock contention. Many threads
BLOCKEDon, or parked waiting for, the same lock points to one hot lock. On Java 25 the dump names the owner throughmonitorsOwnedorparkBlocker. - Slow dependencies. Thousands of virtual threads with the same stack inside a socket read from one client library show which downstream service is slow.
- Leaks. Thread counts in a container that keep growing between dumps show tasks that never finish.
- Combine with metrics. A dump shows what threads are doing at one instant. Pair it with CPU, heap and latency data, and with JFR (article 16) for what happened over time.
What's current
jcmd Thread.dump_to_fileandHotSpotDiagnosticMXBean.dumpThreadswere introduced with the virtual-thread preview in Java 19 (JEP 425) and are standard since Java 21. They're the only dumps that include virtual threads.- Java 25 added thread state and lock information (
monitorsOwned,waitingOn,parkBlocker) to these dumps. Java 21 listed only names and stacks. - Structured-concurrency scopes appear as owned containers, which makes the parent-child task tree visible in a dump (Part 4).
- Recent JDKs, including Java 25, add
jcmd <pid> Thread.vthread_scheduler, which prints the virtual-thread scheduler's state (parallelism, pool size, queued tasks), andThread.vthread_pollers, which lists the I/O pollers.
Common questions
Why doesn't jstack show virtual threads?
The traditional dump walks the JVM's list of platform threads, and an unmounted virtual thread isn't one: it's an object on the heap. Including millions of them in the classic format, at a safepoint, would be impractical, so the JDK added a separate dump that writes to a file.
Is taking a thread dump with a million virtual threads safe? It's expensive: the dump has to walk every stack, and the file can be hundreds of megabytes. It doesn't stop the application for the whole time, but expect a noticeable pause and a large file. Dump on demand, not on a schedule.
What does an empty thread name mean? That the virtual thread was created without a name, which is the default. Name threads with a builder or factory if you want readable dumps.
Why does the API insist on an absolute path? The dump can be requested remotely over JMX, where a relative path would be ambiguous. Requiring an absolute path that doesn't exist yet also prevents overwriting files by accident.
Summary
jstackandjcmd Thread.printshow only platform threads. Usejcmd <pid> Thread.dump_to_fileorHotSpotDiagnosticMXBean.dumpThreadsfor virtual threads.- Both produce plain text or JSON in a file. JSON groups threads into a tree of thread containers: the root, executors, the scheduler's pool and structured task scopes.
- On Java 25, each entry includes the state, a
virtualflag, the stack and lock information. - Name virtual threads that do distinct jobs so dumps are readable.
- Analyze dumps with tools or scripts: group by stack and state, compare several dumps, and combine them with JFR and metrics.
Practice questions
A service on Java 21 hangs. jstack shows eight ForkJoinPool-1-worker threads parked, and nothing else interesting. What should the engineer do next?
Answer
Take a dump that includes virtual threads: jcmd <pid> Thread.dump_to_file -format=json dump.json (or the plain format). The idle carriers only show that no virtual thread is running. The new dump shows what the virtual threads are waiting on.
Why does this call fail, and how do you fix it?
ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class)
.dumpThreads("threads.json", HotSpotDiagnosticMXBean.ThreadDumpFormat.JSON);
Answer
The path is relative, and dumpThreads requires an absolute path to a file that doesn't exist yet. Use something like Path.of("threads.json").toAbsolutePath().toString(), and delete or rename an old file first.
In a JSON dump, 4,000 virtual threads in one ThreadPerTaskExecutor container all show the same stack: a socket read (sun.nio.ch.NioSocketImpl.read) called from a payment client. What does that suggest?
Answer
The payment service (or the network path to it) is slow or unresponsive, and requests are piling up waiting for it. Virtual threads make that waiting cheap, but the service needs a timeout on those calls and probably a bulkhead or rate limit (article 12) so a slow dependency can't accumulate unlimited requests.
Previous: Monitoring Java Virtual Threads With JFR: Pinning, Lifecycle and Submit-Failed Events
Next: Migrating to Java Virtual Threads: A Practical Checklist

More in Modern Java Concurrency
Quiz: Java Concurrency Changes From Java 21 to Java 28
Sixteen questions on what changed release by release: virtual threads final in 21, synchronized without pinning in 24, scoped values final in 25, and the long road of structured concurrency from subclasses to open() and Joiner, the Java 26 and 27 refinements, and final in 28.
Quiz: Java Concurrency's Delicate Details
Twenty edge cases of modern Java concurrency that surprise experienced developers: virtual thread priorities and names, semaphores without owners, null scoped values, cancelling a CompletableFuture, pool sizing with unbounded queues, and StructuredTaskScope's strict rules.
Quiz: Java Concurrency Interview Questions (Virtual Threads, Structured Concurrency, Scoped Values)
Twenty questions of the kind interviewers ask about modern Java concurrency: platform vs virtual threads, pinning, pools and limits, CompletableFuture, structured concurrency, scoped values vs ThreadLocal, reactive programming, and production diagnostics.
Quiz: Modern Java Concurrency Final Exam (All 7 Parts)
The final exam for the modern Java concurrency course: thirty questions across all seven parts, from platform threads, pools and CompletableFuture through virtual threads, the scheduler, structured concurrency, scoped values, reactive streams and frameworks.
Discussion
Sign in to ask a question or share what you learned.