一、基本情况
jstack(JVM Stack Trace):用于生成虚拟机指定进程当前时刻的线程快照(虚拟机堆栈跟踪)。线程快照就是当前虚拟机内指定进程的每一条线程正在执行的方法堆栈的集合。
生成线程快照的作用:可用于定位线程出现长时间停顿的原因,如线程间死锁、死循环、请求外部资源导致的长时间等待等问题。这些都是导致线程长时间停顿的常见原因。当线程出现停顿时,就可以用jstack显示各个线程调用的堆栈情况。
官方帮助文档:
https://docs.oracle.com/en/java/javase/11/tools/jstack.html
在thread dump中,要留意下面几种状态
- 死锁,Deadlock(重点关注)
- 等待资源,Waiting on condition(重点关注)
- 等待获取监视器,Waiting on monitor entry(重点关注)
- 阻塞,Blocked(重点关注)
- 执行中,Runnable
- 暂停,Suspended
二、基本语法
它的基本使用语法为:
jstack option pid
jstack管理远程进程的话,需要在远程程序的启动参数中增加:
-Djava.rmi.server.hostname=… . .
-Dcom.sun.management .jmxremote
-Dcom.sun .management.jmxremote.port=8888
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false
2.1、举例
package studies.jvm.jstack;
/**
* @Date: 2021/5/12
* @Time: 13:22
* @BelongsProject base
* @BelongsPackage studies.jvm.jstack
* 演示死锁问题
*/
public class TreadDeadLock {
public static void main(String[] args) {
StringBuilder s1 = new StringBuilder();
StringBuilder s2 = new StringBuilder();
new Thread(() -> {
synchronized (s1){
s1.append("a");
s2.append("1");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (s2){
s1.append("b");
s2.append("2");
System.out.println("s1-1 "+s1);
System.out.println("s2-1 "+s2);
}
}
}).start();
new Thread(new Runnable(){
@Override
public void run() {
synchronized (s2){
s1.append("c");
s2.append("3");
synchronized (s1){
s1.append("d");
s2.append("4");
System.out.println("s1-2 "+s1);
System.out.println("s2-2 "+s2);
}
}
}
}).start();
}
}
控制台输出
C:\Users\dell>jps
2464
25616 Jps
27808 TreadDeadLock
4996 Launcher
C:\Users\dell>jstack 27808
2021-05-12 14:00:03
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.291-b10 mixed mode):
"DestroyJavaVM" #14 prio=5 os_prio=0 tid=0x000002b4f4569800 nid=0x74dc waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Thread-1" #13 prio=5 os_prio=0 tid=0x000002b4fbd5b800 nid=0x2d88 waiting for monitor entry [0x000000e3297ff000]
java.lang.Thread.State: BLOCKED (on object monitor)
at studies.jvm.jstack.TreadDeadLock$1.run(TreadDeadLock.java:47)
- waiting to lock <0x00000007167d7810> (a java.lang.StringBuilder)
- locked <0x00000007167d7858> (a java.lang.StringBuilder)
at java.lang.Thread.run(Thread.java:748)
"Thread-0" #12 prio=5 os_prio=0 tid=0x000002b4fbd55000 nid=0x3f08 waiting for monitor entry [0x000000e3296fe000]
java.lang.Thread.State: BLOCKED (on object monitor)
at studies.jvm.jstack.TreadDeadLock.lambda$main$0(TreadDeadLock.java:28)
- waiting to lock <0x00000007167d7858> (a java.lang.StringBuilder)
- locked <0x00000007167d7810> (a java.lang.StringBuilder)
at studies.jvm.jstack.TreadDeadLock$$Lambda$1/501263526.run(Unknown Source)
at java.lang.Thread.run(Thread.java:748)
"Service Thread" #11 daemon prio=9 os_prio=0 tid=0x000002b4fbb0e000 nid=0x77fc runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C1 CompilerThread3" #10 daemon prio=9 os_prio=2 tid=0x000002b4fb9ef000 nid=0x15cc waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread2" #9 daemon prio=9 os_prio=2 tid=0x000002b4fb9e2800 nid=0x908 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread1" #8 daemon prio=9 os_prio=2 tid=0x000002b4fb9df000 nid=0x60c4 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread0" #7 daemon prio=9 os_prio=2 tid=0x000002b4fb9de000 nid=0x53dc waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Monitor Ctrl-Break" #6 daemon prio=5 os_prio=0 tid=0x000002b4fba80800 nid=0x7190 runnable [0x000000e328ffe000]
java.lang.Thread.State: RUNNABLE
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
at java.net.SocketInputStream.read(SocketInputStream.java:171)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
- locked <0x0000000716859f98> (a java.io.InputStreamReader)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:161)
at java.io.BufferedReader.readLine(BufferedReader.java:324)
- locked <0x0000000716859f98> (a java.io.InputStreamReader)
at java.io.BufferedReader.readLine(BufferedReader.java:389)
at com.intellij.rt.execution.application.AppMainV2$1.run(AppMainV2.java:48)
"Attach Listener" #5 daemon prio=5 os_prio=2 tid=0x000002b4f985c800 nid=0x7704 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Signal Dispatcher" #4 daemon prio=9 os_prio=2 tid=0x000002b4f985b800 nid=0xd00 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Finalizer" #3 daemon prio=8 os_prio=1 tid=0x000002b4f982d800 nid=0x16c8 in Object.wait() [0x000000e328cff000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x0000000716588ee0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:144)
- locked <0x0000000716588ee0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:165)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:216)
"Reference Handler" #2 daemon prio=10 os_prio=2 tid=0x000002b4f9825000 nid=0x6af4 in Object.wait() [0x000000e328bfe000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x0000000716586c00> (a java.lang.ref.Reference$Lock)
at java.lang.Object.wait(Object.java:502)
at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
- locked <0x0000000716586c00> (a java.lang.ref.Reference$Lock)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)
"VM Thread" os_prio=2 tid=0x000002b4f97fa000 nid=0xb24 runnable
"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x000002b4f4580800 nid=0x5f88 runnable
"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x000002b4f4582000 nid=0x5b58 runnable
"GC task thread#2 (ParallelGC)" os_prio=0 tid=0x000002b4f4583000 nid=0x1a68 runnable
"GC task thread#3 (ParallelGC)" os_prio=0 tid=0x000002b4f4584800 nid=0x46d0 runnable
"GC task thread#4 (ParallelGC)" os_prio=0 tid=0x000002b4f4586800 nid=0x56e0 runnable
"GC task thread#5 (ParallelGC)" os_prio=0 tid=0x000002b4f4587800 nid=0x5894 runnable
"GC task thread#6 (ParallelGC)" os_prio=0 tid=0x000002b4f458b800 nid=0x76c4 runnable
"GC task thread#7 (ParallelGC)" os_prio=0 tid=0x000002b4f458c800 nid=0x6a20 runnable
"VM Periodic Task Thread" os_prio=2 tid=0x000002b4fbb7a800 nid=0x5d0 waiting on condition
JNI global references: 314
Found one Java-level deadlock:
=============================
"Thread-1":
waiting to lock monitor 0x000002b4f982ac28 (object 0x00000007167d7810, a java.lang.StringBuilder),
which is held by "Thread-0"
"Thread-0":
waiting to lock monitor 0x000002b4f982c0c8 (object 0x00000007167d7858, a java.lang.StringBuilder),
which is held by "Thread-1"
Java stack information for the threads listed above:
===================================================
"Thread-1":
at studies.jvm.jstack.TreadDeadLock$1.run(TreadDeadLock.java:47)
- waiting to lock <0x00000007167d7810> (a java.lang.StringBuilder)
- locked <0x00000007167d7858> (a java.lang.StringBuilder)
at java.lang.Thread.run(Thread.java:748)
"Thread-0":
at studies.jvm.jstack.TreadDeadLock.lambda$main$0(TreadDeadLock.java:28)
- waiting to lock <0x00000007167d7858> (a java.lang.StringBuilder)
- locked <0x00000007167d7810> (a java.lang.StringBuilder)
at studies.jvm.jstack.TreadDeadLock$$Lambda$1/501263526.run(Unknown Source)
at java.lang.Thread.run(Thread.java:748)
Found 1 deadlock.
C:\Users\dell>
很明显两个线程都处于上锁状态
可以看出两个线程相互持有锁的状态
例子2:
package studies.jvm.jstack;
/**
* @Date: 2021/5/12
* @Time: 19:13
* @BelongsProject base
* @BelongsPackage studies.jvm.jstack
* 演示线程:TIMED_WAITING
*/
public class TreadSleepTest {
public static void main(String[] args) {
System.out.println("hello - 1");
try {
Thread.sleep(1000 * 60 * 10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("hello - 2");
}
}
输出内容如下:
2021-05-12 19:17:22
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.291-b10 mixed mode):
"Service Thread" #11 daemon prio=9 os_prio=0 tid=0x0000020ed273f000 nid=0x67a8 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C1 CompilerThread3" #10 daemon prio=9 os_prio=2 tid=0x0000020ed2619800 nid=0x6710 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread2" #9 daemon prio=9 os_prio=2 tid=0x0000020ed2614800 nid=0x6ca4 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread1" #8 daemon prio=9 os_prio=2 tid=0x0000020ed260e000 nid=0x67c8 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread0" #7 daemon prio=9 os_prio=2 tid=0x0000020ed26b5800 nid=0x6dcc waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Monitor Ctrl-Break" #6 daemon prio=5 os_prio=0 tid=0x0000020ed26b4000 nid=0x577c runnable [0x00000062905fe000]
java.lang.Thread.State: RUNNABLE
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
at java.net.SocketInputStream.read(SocketInputStream.java:171)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
- locked <0x0000000716859e90> (a java.io.InputStreamReader)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:161)
at java.io.BufferedReader.readLine(BufferedReader.java:324)
- locked <0x0000000716859e90> (a java.io.InputStreamReader)
at java.io.BufferedReader.readLine(BufferedReader.java:389)
at com.intellij.rt.execution.application.AppMainV2$1.run(AppMainV2.java:48)
"Attach Listener" #5 daemon prio=5 os_prio=2 tid=0x0000020ed04be000 nid=0x4e04 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Signal Dispatcher" #4 daemon prio=9 os_prio=2 tid=0x0000020ed04bd800 nid=0x6b90 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Finalizer" #3 daemon prio=8 os_prio=1 tid=0x0000020ed048e800 nid=0x5b34 in Object.wait() [0x00000062902ff000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x0000000716588ee0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:144)
- locked <0x0000000716588ee0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:165)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:216)
"Reference Handler" #2 daemon prio=10 os_prio=2 tid=0x0000020ed047f000 nid=0x4ec0 in Object.wait() [0x00000062901ff000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x0000000716586c00> (a java.lang.ref.Reference$Lock)
at java.lang.Object.wait(Object.java:502)
at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
- locked <0x0000000716586c00> (a java.lang.ref.Reference$Lock)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)
"main" #1 prio=5 os_prio=0 tid=0x0000020eac46a000 nid=0x3b2c waiting on condition [0x000000628f7ff000]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at studies.jvm.jstack.TreadSleepTest.main(TreadSleepTest.java:13)
"VM Thread" os_prio=2 tid=0x0000020ed0455000 nid=0x43b0 runnable
"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x0000020eac482000 nid=0x7134 runnable
"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x0000020eac483800 nid=0x4b10 runnable
"GC task thread#2 (ParallelGC)" os_prio=0 tid=0x0000020eac484800 nid=0x5858 runnable
"GC task thread#3 (ParallelGC)" os_prio=0 tid=0x0000020eac486000 nid=0x5f94 runnable
"GC task thread#4 (ParallelGC)" os_prio=0 tid=0x0000020eac488000 nid=0x5490 runnable
"GC task thread#5 (ParallelGC)" os_prio=0 tid=0x0000020eac489000 nid=0x423c runnable
"GC task thread#6 (ParallelGC)" os_prio=0 tid=0x0000020eac48d000 nid=0x730c runnable
"GC task thread#7 (ParallelGC)" os_prio=0 tid=0x0000020eac48e000 nid=0x1b60 runnable
"VM Periodic Task Thread" os_prio=2 tid=0x0000020ed263e800 nid=0x72cc waiting on condition
JNI global references: 12
例子3:
package studies.jvm.jstack;
/**
* @Date: 2021/5/12
* @Time: 19:21
* @BelongsProject base
* @BelongsPackage studies.jvm.jstack
* 演示线程同步的问题
*/
public class ThreadSyncTest {
public static void main(String[] args) {
Number number = new Number();
Thread t1 = new Thread(number);
Thread t2 = new Thread(number);
t1.setName("线程1");
t2.setName("线程2");
t1.start();
t2.start();
}
}
class Number implements Runnable{
private int number = 1;
@Override
public void run() {
while (true){
synchronized (this){
if (number <= 1000){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+" : "+number);
number++;
}else {
break;
}
}
}
}
}
输出内容如下:
“线程2” 在等待的状态,”线程1” 在握锁的状态
2021-05-12 19:28:43
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.291-b10 mixed mode):
"DestroyJavaVM" #14 prio=5 os_prio=0 tid=0x000001cd613fa000 nid=0x6fe8 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"线程2" #13 prio=5 os_prio=0 tid=0x000001cd77b2c000 nid=0x3528 waiting on condition [0x0000003c220ff000]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at studies.jvm.jstack.Number.run(ThreadSyncTest.java:32)
- locked <0x00000007167d9378> (a studies.jvm.jstack.Number)
at java.lang.Thread.run(Thread.java:748)
"线程1" #12 prio=5 os_prio=0 tid=0x000001cd77ab7800 nid=0x1f34 waiting for monitor entry [0x0000003c21fff000]
java.lang.Thread.State: BLOCKED (on object monitor)
at studies.jvm.jstack.Number.run(ThreadSyncTest.java:29)
- waiting to lock <0x00000007167d9378> (a studies.jvm.jstack.Number)
at java.lang.Thread.run(Thread.java:748)
"Service Thread" #11 daemon prio=9 os_prio=0 tid=0x000001cd7786d800 nid=0x2784 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C1 CompilerThread3" #10 daemon prio=9 os_prio=2 tid=0x000001cd778e0800 nid=0x61d0 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread2" #9 daemon prio=9 os_prio=2 tid=0x000001cd778df800 nid=0x6960 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread1" #8 daemon prio=9 os_prio=2 tid=0x000001cd778dc800 nid=0x5248 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread0" #7 daemon prio=9 os_prio=2 tid=0x000001cd778db800 nid=0x53d4 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Monitor Ctrl-Break" #6 daemon prio=5 os_prio=0 tid=0x000001cd778d6000 nid=0xc10 runnable [0x0000003c218fe000]
java.lang.Thread.State: RUNNABLE
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
at java.net.SocketInputStream.read(SocketInputStream.java:171)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
- locked <0x0000000716859e90> (a java.io.InputStreamReader)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:161)
at java.io.BufferedReader.readLine(BufferedReader.java:324)
- locked <0x0000000716859e90> (a java.io.InputStreamReader)
at java.io.BufferedReader.readLine(BufferedReader.java:389)
at com.intellij.rt.execution.application.AppMainV2$1.run(AppMainV2.java:48)
"Attach Listener" #5 daemon prio=5 os_prio=2 tid=0x000001cd7572a800 nid=0x5344 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Signal Dispatcher" #4 daemon prio=9 os_prio=2 tid=0x000001cd75729800 nid=0x1e4 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Finalizer" #3 daemon prio=8 os_prio=1 tid=0x000001cd756f7800 nid=0x4bd0 in Object.wait() [0x0000003c215ff000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x0000000716588ee0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:144)
- locked <0x0000000716588ee0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:165)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:216)
"Reference Handler" #2 daemon prio=10 os_prio=2 tid=0x000001cd756f0800 nid=0x6f88 in Object.wait() [0x0000003c214ff000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x0000000716586c00> (a java.lang.ref.Reference$Lock)
at java.lang.Object.wait(Object.java:502)
at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
- locked <0x0000000716586c00> (a java.lang.ref.Reference$Lock)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)
"VM Thread" os_prio=2 tid=0x000001cd756c5800 nid=0x6ffc runnable
"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x000001cd61411000 nid=0x5bf8 runnable
"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x000001cd61412800 nid=0x38c0 runnable
"GC task thread#2 (ParallelGC)" os_prio=0 tid=0x000001cd61413800 nid=0x2d44 runnable
"GC task thread#3 (ParallelGC)" os_prio=0 tid=0x000001cd61415000 nid=0x305c runnable
"GC task thread#4 (ParallelGC)" os_prio=0 tid=0x000001cd61417000 nid=0x4e4c runnable
"GC task thread#5 (ParallelGC)" os_prio=0 tid=0x000001cd61418000 nid=0x75e8 runnable
"GC task thread#6 (ParallelGC)" os_prio=0 tid=0x000001cd6141c000 nid=0x6d10 runnable
"GC task thread#7 (ParallelGC)" os_prio=0 tid=0x000001cd6141d000 nid=0x1dc0 runnable
"VM Periodic Task Thread" os_prio=2 tid=0x000001cd77a2c800 nid=0x4d48 waiting on condition
JNI global references: 12
2.2、基本参数
1、option参数: -F
2、option参数: -l
3、option参数: -m
4、option参数: -h
三、用java代码监控线程
代码:
package studies.jvm.jstack;
import java.util.Map;
import java.util.Set;
/**
* @Date: 2021/5/12
* @Time: 19:39
* @BelongsProject base
* @BelongsPackage studies.jvm.jstack
*/
public class AllStackTrace {
public static void main(String[] args) {
//追踪当前进程中的所有的线程
Map<Thread, StackTraceElement[]> all = Thread.getAllStackTraces();
Set<Map.Entry<Thread, StackTraceElement[]>> entries = all.entrySet();
for (Map.Entry<Thread, StackTraceElement[]> en : entries){
Thread t = en.getKey();
StackTraceElement[] v = en.getValue();
System.out.println("【Thread name is : "+t.getName()+" 】");
for (StackTraceElement s : v){
System.out.println("\t"+s.toString());
}
}
}
}
输出:
【Thread name is : Monitor Ctrl-Break 】
java.net.PlainSocketImpl.<init>(PlainSocketImpl.java:98)
java.net.SocksSocketImpl.<init>(SocksSocketImpl.java:56)
java.net.Socket.setImpl(Socket.java:520)
java.net.Socket.<init>(Socket.java:441)
java.net.Socket.<init>(Socket.java:228)
com.intellij.rt.execution.application.AppMainV2$1.run(AppMainV2.java:43)
【Thread name is : Signal Dispatcher 】
【Thread name is : Finalizer 】
java.lang.Object.wait(Native Method)
java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:144)
java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:165)
java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:216)
【Thread name is : Attach Listener 】
【Thread name is : main 】
java.lang.Thread.dumpThreads(Native Method)
java.lang.Thread.getAllStackTraces(Thread.java:1610)
studies.jvm.jstack.AllStackTrace.main(AllStackTrace.java:19)
【Thread name is : Reference Handler 】
java.lang.Object.wait(Native Method)
java.lang.Object.wait(Object.java:502)
java.lang.ref.Reference.tryHandlePending(Reference.java:191)
java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)
Process finished with exit code 0