linux打印堆栈命令 打印java堆栈信息

kill-3生成的线程堆栈怎么查看第一步:在终端运行Java程序
第二步:通过命令 pidof java 找到已经启动的java进程的ID,选择需要查看的java程序的进程ID
第三步:使用命令 kill -3 java进行的 pid 打印出java程序的线程堆栈信息
第四步:通常情况下运行的项目可能会比较大,那么这个时候打印的堆栈信息可能会有几千到几万行,为了方便查看,我们往往需要将输出内容进行重定向
使用linux下的重定向命令方式即可:例如: demo.shrun.log 21 将输出信息重定向到 run.log中 。
注:在操作系统中,0 1 2分别对应着不同的含义,如下:
0 : 标准输入 , 即:C中的stdin ,java中的System.in
1 : 标准输出, 即:C中的stdout ,java中的System.out
2 : 错误输出, 即:C中的stderr  ,  java中的System.err
Demo:
----------------------------------------------------------------------------------------------
Sources Code :
public class PrintThreadTrace {
Object obj1 = new Object();
Object obj2 = new Object();
public void func1(){
synchronized (obj1){
func2();
}
}
public void func2(){
synchronized (obj2){
while(true){
System.out.print("");
}
}
}
public static void main(String[] args){
PrintThreadTrace ptt = new PrintThreadTrace();
ptt.func1();
}
}
----------------------------------------------------------------------------------------------------------------
按照步骤操作后的打印输出信息:
Full thread dump Java HotSpot(TM) 64-Bit Server VM (24.79-b02 mixed mode):
"Service Thread" daemon prio=10 tid=0x00007fdc880a9000 nid=0x12a4 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread1" daemon prio=10 tid=0x00007fdc880a7000 nid=0x12a3 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread0" daemon prio=10 tid=0x00007fdc880a4000 nid=0x12a2 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"JDWP Command Reader" daemon prio=10 tid=0x00007fdc50001000 nid=0x1299 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"JDWP Event Helper Thread" daemon prio=10 tid=0x00007fdc880a1800 nid=0x1298 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"JDWP Transport Listener: dt_socket" daemon prio=10 tid=0x00007fdc8809e000 nid=0x1297 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Signal Dispatcher" daemon prio=10 tid=0x00007fdc88091000 nid=0x1296 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
【linux打印堆栈命令 打印java堆栈信息】"Finalizer" daemon prio=10 tid=0x00007fdc88071800 nid=0x1295 in Object.wait() [0x00007fdc77ffe000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on 0x00000000ecb04858 (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:135)
- locked 0x00000000ecb04858 (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:151)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209)
"Reference Handler" daemon prio=10 tid=0x00007fdc8806f800 nid=0x1294 in Object.wait() [0x00007fdc7c10b000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on 0x00000000ecb04470 (a java.lang.ref.Reference$Lock)
at java.lang.Object.wait(Object.java:503)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:133)
- locked 0x00000000ecb04470 (a java.lang.ref.Reference$Lock)
"main" prio=10 tid=0x00007fdc8800b800 nid=0x128e runnable [0x00007fdc8fef7000]

推荐阅读