Sub topic 12
Seeing Virtual Threads: Thread Dumps and JFR
When a program has thousands of virtual threads, you need a way to see what they are doing. The JDK has two standard tools for this. A thread dump is a snapshot of every thread and the code it is running at that moment. JDK Flight Recorder (JFR) is a recorder built into the JVM that logs events, such as a thread starting or getting stuck, while your program runs.
This chapter shows how to use both tools with virtual threads.
A Program to Watch
You need a running program before you can inspect it. This one starts 1,000 named virtual threads that sleep for a minute, so you have time to look at them.
import java.time.Duration;
import java.util.concurrent.ThreadFactory;
public class Watchable {
public static void main(String[] args) throws InterruptedException {
System.out.println("PID: " + ProcessHandle.current().pid());
ThreadFactory factory = Thread.ofVirtual().name("sleeper-", 0).factory();
Thread[] threads = new Thread[1_000];
for (int i = 0; i < threads.length; i++) {
threads[i] = factory.newThread(() -> {
try {
Thread.sleep(Duration.ofSeconds(60));
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
});
threads[i].start();
}
for (Thread t : threads) {
t.join();
}
System.out.println("Done");
}
}Run it with java Watchable.java. It prints its process ID (PID), which is the number the operating system uses to identify the program. You need the PID in the next step.
Taking a Thread Dump
The older tool jstack does not list virtual threads. Use jcmd instead. While Watchable is running, open a second terminal and run this, replacing 12345 with your PID:
jcmd 12345 Thread.dump_to_file -format=json threads.jsonThis writes every thread, virtual threads included, to threads.json. JSON is a structured text format that scripts can read easily. If you leave out -format=json, you get plain text, which is easier to read yourself:
jcmd 12345 Thread.dump_to_file threads.txtOpen the file and search for sleeper-. You will find 1,000 entries. Each one shows a stack trace ending in Thread.sleep.
Recording Events with JFR
A thread dump shows a single moment. JFR records what happens over a period of time. Start a recording when you launch the program:
java -XX:StartFlightRecording=filename=rec.jfr,duration=30s Watchable.javaThe recording is saved to rec.jfr after 30 seconds. To print the events in it, use the jfr tool that comes with the JDK:
jfr print --events jdk.VirtualThreadPinned rec.jfrThe JDK defines these virtual thread events:
| Event | What it records | On by default? |
|---|---|---|
| `jdk.VirtualThreadStart` | A virtual thread started | No |
| `jdk.VirtualThreadEnd` | A virtual thread ended | No |
| `jdk.VirtualThreadPinned` | A virtual thread blocked while pinned to its carrier for more than 20 ms | Yes |
| `jdk.VirtualThreadSubmitFailed` | The scheduler could not start or resume a virtual thread | Yes |
The start and end events are off by default because a busy program could create millions of them.
Which Tool to Use
Use a thread dump when something looks stuck and you want to see where every thread is waiting. Use JFR when you want to find problems such as pinning that come and go over time.
Exercise
You run jstack on a program that uses 5,000 virtual threads, but none of them appear in the output. Which command should you run instead to see them?
Show answer
Run jcmd <PID> Thread.dump_to_file threads.txt (add -format=json for JSON output). Unlike jstack, this command includes virtual threads in the dump.
Exercise
You want JFR to record every virtual thread that starts. Why won't a default recording show these events?
Show answer
The jdk.VirtualThreadStart event is disabled by default because a busy program can create a huge number of them. You must turn it on in your JFR settings to record it.