jstack简介

jstack:Java进程中线程的堆栈信息跟踪工具

功能简介

jstack常用来打印Java进程/core文件/远程调试端口的Java线程堆栈跟踪信息,包含当前虚拟机中所有线程正在执行的方法堆栈信息的集合。

主要用来定位线程出现长时间停顿的原因,如线程间死锁、死循环、请求外部资源导致的长时间等待。

命令格式

1 jstack [ options ] pid                                      //Java进程
2 jstack [ options ] executable core                          //core文件
3 jstack [ options ] [ server-id@ ] remote-hostname-or-IP     //远程调试端口

其中options选项可有

选项 作用
-F 当正常输出的请求不被响应时,强制输出线程堆栈
-l 除了堆栈外,显示关于锁的附加信息
-m 如果调用到本地方法的话,可以显示C/C++的堆栈信息

其他说明

1、When the specified process is running on a 64-bit Java Virtual Machine, you might need to specify the -J-d64 option, for example: jstack -J-d64 -m pid.

2、In mixed mode stack trace, the -m option does not work with the remote debug server.

3、In Windows Systems where the dbgeng.dll file is not present, Debugging Tools For Windows must be installed so these tools work.

输出格式

jstack的输出是该进程下的所有线程的堆栈集合,下面是一个线程的堆栈快照信息:

 1 "pool-1-thread-3" #12 prio=5 os_prio=0 tid=0x00007fc99412f000 nid=0x9bc in Object.wait() [0x00007fc97c2f2000]
 2    java.lang.Thread.State: WAITING (on object monitor)
 3     at java.lang.Object.wait(Native Method)
 4     - waiting on <0x00000000d7017420> (a com.liang.java.thinkinginjava.concurency.waxomatic.Car)
 5     at java.lang.Object.wait(Object.java:502)
 6     at com.liang.java.thinkinginjava.concurency.waxomatic.Car.waitForBuffing(WaxOMatic.java:47)
 7     - locked <0x00000000d7017420> (a com.liang.java.thinkinginjava.concurency.waxomatic.Car)
 8     at com.liang.java.thinkinginjava.concurency.waxomatic.WaxOn.run(WaxOMatic.java:61)
 9     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
10     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
11     at java.lang.Thread.run(Thread.java:745)
12 
13    Locked ownable synchronizers:
14     - <0x00000000d729cdb0> (a java.util.concurrent.ThreadPoolExecutor$Worker)

其中 "pool-1-thread-3" 是线程名称

 prio=5 是该线程JVM中的优先级

 os_prio=0 是该线程在OS中的优先级

 tid=0x00007fc99412f000 是JVM内的thread id (Java-level thread ID)

 nid=0x9bc 是Native thread ID,本地操作系统相关的线程id

This ID is highly platform dependent. It's the nid in jstack thread dumps.

  • On Windows, it's simply the OS-level thread ID within a process.

  • On Linux, it's the pid of the thread (which in turn is a light-weight process).

  • On Solaris, it's the thread as returned by thr_self().

  • On Mac OS X, it is said to be the native pthread_t value.

线程状态

 在jstack输出的第二行为线程的状态,在JVM中线程状态使用枚举 java.lang.Thread.State 来表示,State的定义如下:

 1 /**
 2  * A thread can be in only one state at a given point in time.
 3  * These states are virtual machine states which do not reflect any operating system thread states.
 4  */
 5 public enum State {
 6     /**
 7      * A thread that has not yet started is in this state.
 8      */
 9     NEW,
10 
11     /**
12      * A thread executing in the Java virtual machine is in this state.
13      */
14     RUNNABLE,
15 
16     /**
17      * A thread that is blocked waiting for a monitor lock is in this state.
18      */
19     BLOCKED,
20 
21     /**
22      * A thread that is waiting indefinitely for another thread to perform a particular action is in this state.
23      */
24     WAITING,
25 
26     /**
27      * A thread that is waiting for another thread to perform an action for up to a specified waiting time is in this state.
28      */
29     TIMED_WAITING,
30 
31     /**
32      * A thread that has exited is in this state.
33      */
34     TERMINATED;
35 }

这个状态是指的JVM中的状态,与任何操作系统的线程状态都没有任何关系。

其中RUNNABLE表示:线程在JVM中是可以执行的了,但是他可能还在等待着某些操作系统资源,如:cpu

其中BLOCKED表示:表示线程在等待或被notify后等待重新进入synchronized代码块/方法

其中WAITING表示:表示调用了Object.wait()/Thread.join()/LockSupport.park()方法,等待被另一个线程唤醒。例如一个线程调用了Object.wait(),在等待另一个线程调用Object.notify()/Object.notifyAll();一个线程调用了Thread.join(),在等待另一线程到达terinate状态

其中TIMED_WAITING表示:表示线程调用了Object.sleep(long)/Object.wait(long)/Thread.join(long)/LockSupport.parkNanos()/LockSupport.parkUntil()等,等待一段时间后就会自动结束的方法;

也就是说BLOCKED在等待锁,WAITING在等待被其他线程,TIMED_WAITING是带闹钟的WAITING

下面献上网上找到的一个高清状态转换图:

样例

java线程的获得:使用jps -l,或者使用top命令查看使用cpu/memory最多的进程id,或者ps -aux | grep java

进程id的获得:top -Hp pid,找到改线程下的最繁忙的进程id(十进制要转换成16进制:printf "%x " tid)

jstack -l pid | grep tid

关于各种线程状态下的例子,网上实在是太多了,一搜一大把! 

参考

https://gist.github.com/rednaxelafx/843622

https://segmentfault.com/a/1190000008506752

原文地址:https://www.cnblogs.com/halu126/p/6731343.html