Virtual threads
Virtual threads were introduced as a preview feature in JDK 19 and officially released in JDK 21. A thread dump can be used to troubleshoot virtual thread issues. The standard jstack command is not suitable for this task because a system can have thousands or even millions of virtual threads. Use the following commands to generate a thread dump that includes information about virtual threads:
jcmd <pid> Thread.dump_to_file -format=json <file>
jcmd <pid> Thread.dump_to_file -format=text <file>The following is an example of the first format:
{
"threadDump": {
"processId": "48390",
"time": "2023-11-01T02:53:36.881597814Z",
"runtimeVersion": "22-internal",
"threadContainers": [
{
"container": "java.util.concurrent.ThreadPerTaskExecutor@672fb5f8",
"parent": "<root>",
"owner": null,
"threads": [
{
"tid": "36",
"name": "",
"stack": [
"java.base\/java.lang.VirtualThread.parkNanos(VirtualThread.java:624)",
"java.base\/java.lang.VirtualThread.sleepNanos(VirtualThread.java:795)",
"java.base\/java.lang.Thread.sleepNanos(Thread.java:489)",
"java.base\/java.lang.Thread.sleep(Thread.java:522)",
"Test.lambda$simple$1(Test.java:16)",
"java.base\/java.util.concurrent.FutureTask.run(FutureTask.java:317)",
"java.base\/java.lang.VirtualThread.run(VirtualThread.java:318)"
]
}
],
"threadCount": "1"
}
...
]
}
}The following is an example of the second format:
#38 "VirtualThread-unparker"
java.base/jdk.internal.misc.Unsafe.park(Native Method)
java.base/java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:269)
java.base/java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:1758)
java.base/java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1182)
java.base/java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:899)
java.base/java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1070)
java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642)
java.base/java.lang.Thread.run(Thread.java:1570)
java.base/jdk.internal.misc.InnocuousThread.run(InnocuousThread.java:186)
#36 "" virtual
java.base/java.lang.VirtualThread.parkNanos(VirtualThread.java:624)
java.base/java.lang.VirtualThread.sleepNanos(VirtualThread.java:795)
java.base/java.lang.Thread.sleepNanos(Thread.java:489)
java.base/java.lang.Thread.sleep(Thread.java:522)
Test.lambda$simple$1(Test.java:16)
java.base/java.util.concurrent.FutureTask.run(FutureTask.java:317)
java.base/java.lang.VirtualThread.run(VirtualThread.java:318)
...The new thread dump format does not include object addresses, locks, Java Native Interface (JNI) statistics, heap statistics, or other information found in traditional thread dumps. ATP supports thread dumps generated by the preceding commands.
After opening a vt.log file on the ATP thread analysis page, the Thread Information tab displays a list of threads, including platform threads such as main, Reference Handler, Finalizer, and Signal Dispatcher, as well as the <Virtual Threads> entry at the bottom, indicating that ATP can identify and display virtual thread information.
After selecting the Virtual Threads new tab on the ATP virtual thread analysis page, a table displays the hierarchical structure of thread containers. The table columns include Thread Container, Parent Container, Owner, and Thread Count. You can expand a specific thread container to view the complete call stack information of the threads it holds.