JDK 19初次引入預覽版虛擬線程(Virtual Thread),并在JDK 21中正式發布,線程轉儲(thread dump)可以用于虛擬線程問題排查。由于虛擬線程可能有數千或者數百萬個,常規的jstack并不適用,目前有如下方式生成包含虛擬線程信息的線程轉儲:
jcmd <pid> Thread.dump_to_file -format=json <file>
jcmd <pid> Thread.dump_to_file -format=text <file>
第一種格式示例如下:
{
"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"
}
...
]
}
}
第二種格式示例如下:
#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)
...
新的線程轉儲格式不包括對象地址、鎖、JNI 統計信息、堆統計信息以及傳統線程轉儲中出現的其他信息。ATP已經支持如上命令生成的線程轉儲,對應的功能示例如下:
文檔內容是否對您有幫助?