Java Thread Dump 是 JVM 中所有活跃的线程的列表。
关于Java Thread Dump
Java thread dump 在分析应用程序中的 bottlenecks 和 deadlock 情况中非常有用. 在这里,我们将学习通过哪些方式我们可以生成一个 java 程序的 thread dump. 这些说明适用于 *nix 操作系统,但在 Windows 中,步骤可能略有不同。
- VisualVM Profiler: If you are analyzing application for slowness, you must use a profiler. We can generate thread dump for any process using VisualVM profiler very easily. You just need to right click on the running process and click on "Thread Dump" option to generate it.
- jstack: Java comes with jstack tool through which we can generate thread dump for a java process. This is a two step process.
- Find out the PID of the java process using
ps -eaf | grep java
command - Run jstack tool as
jstack PID
to generate the thread dump output to console, you can append thread dump output to file using command "jstack PID >> mydumps.tdump
"
- Find out the PID of the java process using
- We can use
kill -3 PID
command to generate the thread dump. This is slightly different from other ways to generate thread dump. When kill command is issued, thread dump is generated to the System out of the program. So if it's a java program with console as system out, the thread dump will get printed on the console. If the java program is a Tomcat server with system out ascatalina.out
, then thread dump will be generated in the file. - Java 8 has introduced
jcmd
utility. You should use this instead of jstack if you are on Java 8 or higher. Command to generate thread dump using jcmd isjcmd PID Thread.print
.
上面有四种不同的方法来生成 Java 中的 thread dump. 我通常更喜欢 jstack 或 jcmd 命令来生成 thread dump 并分析。
Java Thread Dump 示例
在我最近的帖子中,我解释了关于Java Timer(/社区/教程/java-timer-timertask-例子Java Timer TimerTask
),这里是为同一个程序生成的线程漏洞。
12012-12-26 22:28:39
2Full thread dump Java HotSpot(TM) 64-Bit Server VM (23.5-b02 mixed mode):
3
4"Attach Listener" daemon prio=5 tid=0x00007fb7d8000000 nid=0x4207 waiting on condition [0x0000000000000000]
5 java.lang.Thread.State: RUNNABLE
6
7"Timer-0" daemon prio=5 tid=0x00007fb7d4867000 nid=0x5503 waiting on condition [0x00000001604d9000]
8 java.lang.Thread.State: TIMED_WAITING (sleeping)
9 at java.lang.Thread.sleep(Native Method)
10 at com.journaldev.threads.MyTimerTask.completeTask(MyTimerTask.java:19)
11 at com.journaldev.threads.MyTimerTask.run(MyTimerTask.java:12)
12 at java.util.TimerThread.mainLoop(Timer.java:555)
13 at java.util.TimerThread.run(Timer.java:505)
14
15"Service Thread" daemon prio=5 tid=0x00007fb7d482c000 nid=0x5303 runnable [0x0000000000000000]
16 java.lang.Thread.State: RUNNABLE
17
18"C2 CompilerThread1" daemon prio=5 tid=0x00007fb7d482b800 nid=0x5203 waiting on condition [0x0000000000000000]
19 java.lang.Thread.State: RUNNABLE
20
21"C2 CompilerThread0" daemon prio=5 tid=0x00007fb7d4829800 nid=0x5103 waiting on condition [0x0000000000000000]
22 java.lang.Thread.State: RUNNABLE
23
24"Signal Dispatcher" daemon prio=5 tid=0x00007fb7d4828800 nid=0x5003 runnable [0x0000000000000000]
25 java.lang.Thread.State: RUNNABLE
26
27"Finalizer" daemon prio=5 tid=0x00007fb7d4812000 nid=0x3f03 in Object.wait() [0x000000015fd26000]
28 java.lang.Thread.State: WAITING (on object monitor)
29 at java.lang.Object.wait(Native Method)
30 - waiting on <0x0000000140a25798> (a java.lang.ref.ReferenceQueue$Lock)
31 at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:135)
32 - locked <0x0000000140a25798> (a java.lang.ref.ReferenceQueue$Lock)
33 at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:151)
34 at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:177)
35
36"Reference Handler" daemon prio=5 tid=0x00007fb7d4811800 nid=0x3e03 in Object.wait() [0x000000015fc23000]
37 java.lang.Thread.State: WAITING (on object monitor)
38 at java.lang.Object.wait(Native Method)
39 - waiting on <0x0000000140a25320> (a java.lang.ref.Reference$Lock)
40 at java.lang.Object.wait(Object.java:503)
41 at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:133)
42 - locked <0x0000000140a25320> (a java.lang.ref.Reference$Lock)
43
44"main" prio=5 tid=0x00007fb7d5000800 nid=0x1703 waiting on condition [0x0000000106116000]
45 java.lang.Thread.State: TIMED_WAITING (sleeping)
46 at java.lang.Thread.sleep(Native Method)
47 at com.journaldev.threads.MyTimerTask.main(MyTimerTask.java:33)
48
49"VM Thread" prio=5 tid=0x00007fb7d480f000 nid=0x3d03 runnable
50
51"GC task thread#0 (ParallelGC)" prio=5 tid=0x00007fb7d500d800 nid=0x3503 runnable
52
53"GC task thread#1 (ParallelGC)" prio=5 tid=0x00007fb7d500e000 nid=0x3603 runnable
54
55"GC task thread#2 (ParallelGC)" prio=5 tid=0x00007fb7d5800000 nid=0x3703 runnable
56
57"GC task thread#3 (ParallelGC)" prio=5 tid=0x00007fb7d5801000 nid=0x3803 runnable
58
59"GC task thread#4 (ParallelGC)" prio=5 tid=0x00007fb7d5801800 nid=0x3903 runnable
60
61"GC task thread#5 (ParallelGC)" prio=5 tid=0x00007fb7d5802000 nid=0x3a03 runnable
62
63"GC task thread#6 (ParallelGC)" prio=5 tid=0x00007fb7d5802800 nid=0x3b03 runnable
64
65"GC task thread#7 (ParallelGC)" prio=5 tid=0x00007fb7d5803800 nid=0x3c03 runnable
66
67"VM Periodic Task Thread" prio=5 tid=0x00007fb7d481e800 nid=0x5403 waiting on condition
68
69JNI global references: 116
Thread dump 是所有线程的列表,每个输入都显示有关线程的信息,其中包括在出现顺序下列内容。
- Thread Name: Thread Name
- Thread Priority: Thread Priority
- Thread ID: 代表 Thread 4的独特ID。 Thread Status: 提供当前的 thread state,例如RUNNABLE, WAITING, BLOCKED。 在分析 deadlock 时,寻找他们试图获取 lock( _5)的被阻塞的 thread 和资源。 Thread callstack: 提供线程的关键信息堆栈。 这是我们可以看到
这就是在Java中播放的所有内容。