Sub topic 12
Seeing virtual threads in thread dumps and JFR
This chapter assumes you have Java 21 and a Spring Boot 3.2+ MVC app with spring.threads.virtual.enabled=true. You know what pinning is. Here you will learn how to confirm that virtual threads are running and how to find the places where they get stuck.
Why the usual tools fall short
A classic thread dump (jstack or jcmd <pid> Thread.print) lists platform threads only. An application can have 50,000 virtual threads, and a dump that listed all of them would be huge. So virtual threads are left out of the plain-text dump. Your dump will show the carrier threads (named like ForkJoinPool-1-worker-1) and little else about the work they run.
To see virtual threads, you need the JSON dump format, or Java Flight Recorder (JFR) events.
Step 1: Check that the property took effect
Before you look at dumps, confirm requests really run on virtual threads. This endpoint reports it:
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
class ThreadInfoController {
@GetMapping("/thread")
String thread() {
Thread current = Thread.currentThread();
return "virtual=" + current.isVirtual() + ", thread=" + current;
}
}Call /thread. You want virtual=true. If you see false with a name like http-nio-8080-exec-1, the property is not applied. Check the Java version, the Boot version, and the property spelling.
Step 2: Take a JSON thread dump
The jcmd tool can write a dump that includes virtual threads. It can also trace pinning at startup:
java -Djdk.tracePinnedThreads=full -jar app.jar
jcmd <pid> Thread.dump_to_file -format=json threads.jsonThe JSON file groups threads in containers, such as the thread pool or executor that created them. Look for entries with the virtual thread's stack. A thread blocked on a socket read or a JDBC call is normal and healthy. Thousands of virtual threads parked at the same line point to a shared bottleneck, such as a database connection pool or a lock.
A common error is to read the JSON file and expect carrier threads to explain the blocked work. A virtual thread that is unmounted has no carrier. Its stack still appears in the dump, but no platform thread is attached to it.
Step 3: Find pinning
Pinning is when a virtual thread blocks while it cannot unmount. On Java 21, this happens inside a synchronized block or a native frame. The carrier thread is held, and under load you can run out of carriers.
With -Djdk.tracePinnedThreads=full, the JVM prints a stack trace each time a virtual thread blocks while pinned. The trace marks the frames that hold the monitor. Use short for a condensed form.
Typical fix: replace synchronized around blocking code with a ReentrantLock.
import java.util.concurrent.locks.ReentrantLock;
class Counter {
private final ReentrantLock lock = new ReentrantLock();
private long value;
long increment() {
lock.lock();
try {
return ++value;
} finally {
lock.unlock();
}
}
}Step 4: Use JFR for production
Stack traces on stdout are noisy. JFR records the same information as events with little overhead. Two events matter most:
| Event | What it tells you |
|---|---|
| `jdk.VirtualThreadPinned` | A virtual thread blocked while pinned, with its stack and duration. It is recorded when the pause exceeds a threshold (20 ms by default). |
| `jdk.VirtualThreadSubmitFailed` | The scheduler could not start or continue a virtual thread. This is rare and signals resource trouble. |
Start a recording, then open it in JDK Mission Control or print events from the command line:
jcmd <pid> JFR.start name=vt duration=60s filename=vt.jfr
jfr print --events jdk.VirtualThreadPinned vt.jfrGroup the results by the top frames of the stack. A handful of locations usually account for nearly all pinning, and fixing those is enough.
How the pieces fit
Exercise
A plain jstack dump of your Spring Boot app shows only a few ForkJoinPool-1-worker-N threads and none of your request handlers, although /thread reports virtual=true. Why, and what command gives you the missing view?
Show answer
Plain thread dumps list only platform threads, so virtual threads are omitted and you see just the carrier threads. Use jcmd <pid> Thread.dump_to_file -format=json threads.json to get a dump that includes virtual threads.
Exercise
Your JFR recording shows many jdk.VirtualThreadPinned events with the same stack, ending in a synchronized method that calls a JDBC driver. What change do you consider first?
Show answer
Replace the synchronized block around the blocking call with a ReentrantLock (or upgrade the driver or library if the lock is inside it), so the virtual thread can unmount while it waits.