Java jcmd 命令打印线程堆栈

JVM jcmd 诊断工具 About 12,054 words

说明

jcmdThread.printjstack命令功能一致。

查看帮助

jcmd 12345 help Thread.print

输出:

12345:
Thread.print
Print all threads with stacktraces.

Impact: Medium: Depends on the number of threads.

Permission: java.lang.management.ManagementPermission(monitor)

Syntax : Thread.print [options]

Options: (options must be specified using the <key> or <key>=<value> syntax)
        -l : [optional] print java.util.concurrent locks (BOOLEAN, false)
        -e : [optional] print extended thread information (BOOLEAN, false)

格式

-l-e为可选项。

-l:打印AQS的锁情况 -e:打印线程扩展信息

jcmd <pid> Thread.print

案例

Java 代码

public class ThreadPrintDemo {

    public static void main(String[] args) {
        new Thread(() -> {
            synchronized (ThreadPrintDemo.class) {
                System.out.println("AAA sync lock");
                LockSupport.park();
            }
        }, "AAA").start();

        new Thread(() -> {
            synchronized (ThreadPrintDemo.class) {
                System.out.println("BBB sync lock");
                LockSupport.park();
            }
        }, "BBB").start();

        ReentrantLock lock = new ReentrantLock();

        new Thread(() -> {
            try {
                lock.lock();
                System.out.println("CCC aqs lock");
                LockSupport.park();
            } finally {
                lock.unlock();
            }
        }, "CCC").start();

        new Thread(() -> {
            try {
                lock.lock();
                System.out.println("DDD aqs lock");
                LockSupport.park();
            } finally {
                lock.unlock();
            }
        }, "DDD").start();

        LockSupport.park();
    }

}

运行日志

AAA sync lock
CCC aqs lock
...

打印线程

jcmd 12345 Thread.print -l -e

输出结果

12345:
2021-08-05 12:05:27
Full thread dump OpenJDK 64-Bit Server VM (11.0.2+9 mixed mode):

Threads class SMR info:
_java_thread_list=0x00000246fe755be0, length=15, elements={
0x00000246ef82a800, 0x00000246fe2fc800, 0x00000246fe328000, 0x00000246fe38c800,
0x00000246fe38d800, 0x00000246fe390800, 0x00000246fe340000, 0x00000246fe342000,
0x00000246fe4f5800, 0x00000246fe7f4800, 0x00000246fe7f5800, 0x00000246fe744000,
0x00000246fe74b800, 0x00000246fe752800, 0x00000246fe753800
}

"main" #1 prio=5 os_prio=0 cpu=234.38ms elapsed=5870.44s allocated=2086K defined_classes=762 tid=0x00000246ef82a800 nid=0x84cc waiting on condition  [0x00000073a64ff000]
   java.lang.Thread.State: WAITING (parking)
        at jdk.internal.misc.Unsafe.park(java.base@11.0.2/Native Method)
        at java.util.concurrent.locks.LockSupport.park(java.base@11.0.2/LockSupport.java:323)
        at ThreadPrintDemo.main(ThreadPrintDemo.java:43)

   Locked ownable synchronizers:
        - None

"Reference Handler" #2 daemon prio=10 os_prio=2 cpu=0.00ms elapsed=5870.39s allocated=0B defined_classes=0 tid=0x00000246fe2fc800 nid=0x4514 waiting on condition  [0x00000073a6bff000]
   java.lang.Thread.State: RUNNABLE
        at java.lang.ref.Reference.waitForReferencePendingList(java.base@11.0.2/Native Method)
        at java.lang.ref.Reference.processPendingReferences(java.base@11.0.2/Reference.java:241)
        at java.lang.ref.Reference$ReferenceHandler.run(java.base@11.0.2/Reference.java:213)

   Locked ownable synchronizers:
        - None

"Finalizer" #3 daemon prio=8 os_prio=1 cpu=0.00ms elapsed=5870.39s allocated=280B defined_classes=1 tid=0x00000246fe328000 nid=0x6948 in Object.wait()  [0x00000073a6cff000]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(java.base@11.0.2/Native Method)
        - waiting on <0x0000000623008f10> (a java.lang.ref.ReferenceQueue$Lock)
        at java.lang.ref.ReferenceQueue.remove(java.base@11.0.2/ReferenceQueue.java:155)
        - waiting to re-lock in wait() <0x0000000623008f10> (a java.lang.ref.ReferenceQueue$Lock)
        at java.lang.ref.ReferenceQueue.remove(java.base@11.0.2/ReferenceQueue.java:176)
        at java.lang.ref.Finalizer$FinalizerThread.run(java.base@11.0.2/Finalizer.java:170)

   Locked ownable synchronizers:
        - None

"Signal Dispatcher" #4 daemon prio=9 os_prio=2 cpu=0.00ms elapsed=5870.37s allocated=0B defined_classes=0 tid=0x00000246fe38c800 nid=0x49c0 runnable  [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
        - None

"Attach Listener" #5 daemon prio=5 os_prio=2 cpu=31.25ms elapsed=5870.37s allocated=703K defined_classes=127 tid=0x00000246fe38d800 nid=0x7e84 waiting on condition  [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
        - None

"C2 CompilerThread0" #6 daemon prio=9 os_prio=2 cpu=15.63ms elapsed=5870.37s allocated=0B defined_classes=0 tid=0x00000246fe390800 nid=0x71a8 waiting on condition  [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE
   No compile task

   Locked ownable synchronizers:
        - None

"C1 CompilerThread0" #9 daemon prio=9 os_prio=2 cpu=15.63ms elapsed=5870.37s allocated=568B defined_classes=0 tid=0x00000246fe340000 nid=0x7a7c waiting on condition  [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE
   No compile task

   Locked ownable synchronizers:
        - None

"Sweeper thread" #10 daemon prio=9 os_prio=2 cpu=0.00ms elapsed=5870.37s allocated=0B defined_classes=0 tid=0x00000246fe342000 nid=0x7e60 runnable  [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
        - None

"Common-Cleaner" #11 daemon prio=8 os_prio=1 cpu=0.00ms elapsed=5870.32s allocated=0B defined_classes=0 tid=0x00000246fe4f5800 nid=0x3ab8 in Object.wait()  [0x00000073a72ff000]
   java.lang.Thread.State: TIMED_WAITING (on object monitor)
        at java.lang.Object.wait(java.base@11.0.2/Native Method)
        - waiting on <0x0000000623175988> (a java.lang.ref.ReferenceQueue$Lock)
        at java.lang.ref.ReferenceQueue.remove(java.base@11.0.2/ReferenceQueue.java:155)
        - waiting to re-lock in wait() <0x0000000623175988> (a java.lang.ref.ReferenceQueue$Lock)
        at jdk.internal.ref.CleanerImpl.run(java.base@11.0.2/CleanerImpl.java:148)
        at java.lang.Thread.run(java.base@11.0.2/Thread.java:834)
        at jdk.internal.misc.InnocuousThread.run(java.base@11.0.2/InnocuousThread.java:134)

   Locked ownable synchronizers:
        - None

"Monitor Ctrl-Break" #12 daemon prio=5 os_prio=0 cpu=0.00ms elapsed=5870.23s allocated=81K defined_classes=60 tid=0x00000246fe7f4800 nid=0x4b18 runnable  [0x00000073a74fe000]
   java.lang.Thread.State: RUNNABLE
        at java.net.SocketInputStream.socketRead0(java.base@11.0.2/Native Method)
        at java.net.SocketInputStream.socketRead(java.base@11.0.2/SocketInputStream.java:115)
        at java.net.SocketInputStream.read(java.base@11.0.2/SocketInputStream.java:168)
        at java.net.SocketInputStream.read(java.base@11.0.2/SocketInputStream.java:140)
        at sun.nio.cs.StreamDecoder.readBytes(java.base@11.0.2/StreamDecoder.java:284)
        at sun.nio.cs.StreamDecoder.implRead(java.base@11.0.2/StreamDecoder.java:326)
        at sun.nio.cs.StreamDecoder.read(java.base@11.0.2/StreamDecoder.java:178)
        - locked <0x0000000622f04088> (a java.io.InputStreamReader)
        at java.io.InputStreamReader.read(java.base@11.0.2/InputStreamReader.java:185)
        at java.io.BufferedReader.fill(java.base@11.0.2/BufferedReader.java:161)
        at java.io.BufferedReader.readLine(java.base@11.0.2/BufferedReader.java:326)
        - locked <0x0000000622f04088> (a java.io.InputStreamReader)
        at java.io.BufferedReader.readLine(java.base@11.0.2/BufferedReader.java:392)
        at com.intellij.rt.execution.application.AppMainV2$1.run(AppMainV2.java:48)

   Locked ownable synchronizers:
        - None

"Service Thread" #13 daemon prio=9 os_prio=0 cpu=0.00ms elapsed=5870.23s allocated=0B defined_classes=0 tid=0x00000246fe7f5800 nid=0x7918 runnable  [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
        - None

"AAA" #14 prio=5 os_prio=0 cpu=0.00ms elapsed=5870.22s allocated=1008B defined_classes=0 tid=0x00000246fe744000 nid=0x8280 waiting on condition  [0x00000073a77fe000]
   java.lang.Thread.State: WAITING (parking)
        at jdk.internal.misc.Unsafe.park(java.base@11.0.2/Native Method)
        at java.util.concurrent.locks.LockSupport.park(java.base@11.0.2/LockSupport.java:323)
        at ThreadPrintDemo.lambda$main$0(ThreadPrintDemo.java:10)
        - locked <0x0000000622f715a8> (a java.lang.Class for ThreadPrintDemo)
        at ThreadPrintDemo$$Lambda$14/0x0000000800066840.run(Unknown Source)
        at java.lang.Thread.run(java.base@11.0.2/Thread.java:834)

   Locked ownable synchronizers:
        - None

"BBB" #15 prio=5 os_prio=0 cpu=0.00ms elapsed=5870.22s allocated=0B defined_classes=0 tid=0x00000246fe74b800 nid=0x5e38 waiting for monitor entry  [0x00000073a78fe000]
   java.lang.Thread.State: BLOCKED (on object monitor)
        at ThreadPrintDemo.lambda$main$1(ThreadPrintDemo.java:16)
        - waiting to lock <0x0000000622f715a8> (a java.lang.Class for ThreadPrintDemo)
        at ThreadPrintDemo$$Lambda$15/0x0000000800066c40.run(Unknown Source)
        at java.lang.Thread.run(java.base@11.0.2/Thread.java:834)

   Locked ownable synchronizers:
        - None

"CCC" #16 prio=5 os_prio=0 cpu=0.00ms elapsed=5870.22s allocated=2640B defined_classes=1 tid=0x00000246fe752800 nid=0x7a48 waiting on condition  [0x00000073a79ff000]
   java.lang.Thread.State: WAITING (parking)
        at jdk.internal.misc.Unsafe.park(java.base@11.0.2/Native Method)
        at java.util.concurrent.locks.LockSupport.park(java.base@11.0.2/LockSupport.java:323)
        at ThreadPrintDemo.lambda$main$2(ThreadPrintDemo.java:27)
        at ThreadPrintDemo$$Lambda$16/0x0000000800066040.run(Unknown Source)
        at java.lang.Thread.run(java.base@11.0.2/Thread.java:834)

   Locked ownable synchronizers:
        - <0x0000000622f76370> (a java.util.concurrent.locks.ReentrantLock$NonfairSync)

"DDD" #17 prio=5 os_prio=0 cpu=0.00ms elapsed=5870.22s allocated=8456B defined_classes=0 tid=0x00000246fe753800 nid=0xbac waiting on condition  [0x00000073a7aff000]
   java.lang.Thread.State: WAITING (parking)
        at jdk.internal.misc.Unsafe.park(java.base@11.0.2/Native Method)
        - parking to wait for  <0x0000000622f76370> (a java.util.concurrent.locks.ReentrantLock$NonfairSync)
        at java.util.concurrent.locks.LockSupport.park(java.base@11.0.2/LockSupport.java:194)
        at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(java.base@11.0.2/AbstractQueuedSynchronizer.java:885)
        at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireQueued(java.base@11.0.2/AbstractQueuedSynchronizer.java:917)
        at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquire(java.base@11.0.2/AbstractQueuedSynchronizer.java:1240)
        at java.util.concurrent.locks.ReentrantLock.lock(java.base@11.0.2/ReentrantLock.java:267)
        at ThreadPrintDemo.lambda$main$3(ThreadPrintDemo.java:35)
        at ThreadPrintDemo$$Lambda$17/0x0000000800066440.run(Unknown Source)
        at java.lang.Thread.run(java.base@11.0.2/Thread.java:834)

   Locked ownable synchronizers:
        - None

"VM Thread" os_prio=2 cpu=0.00ms elapsed=5870.40s tid=0x00000246fe2f9800 nid=0x8180 runnable

"GC Thread#0" os_prio=2 cpu=0.00ms elapsed=5870.44s tid=0x00000246ef844800 nid=0x3be4 runnable

"G1 Main Marker" os_prio=2 cpu=0.00ms elapsed=5870.44s tid=0x00000246ef8c3800 nid=0x6614 runnable

"G1 Conc#0" os_prio=2 cpu=0.00ms elapsed=5870.43s tid=0x00000246ef8c5000 nid=0x846c runnable

"G1 Refine#0" os_prio=2 cpu=0.00ms elapsed=5870.42s tid=0x00000246fd95f000 nid=0x7544 runnable

"G1 Young RemSet Sampling" os_prio=2 cpu=0.00ms elapsed=5870.42s tid=0x00000246fd95f800 nid=0x3eec runnable
"VM Periodic Task Thread" os_prio=2 cpu=31.25ms elapsed=5870.23s tid=0x00000246fe73f800 nid=0x8440 waiting on condition

JNI global refs: 15, weak refs: 0
Views: 3,319 · Posted: 2021-08-09

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh