jstack分析线程运行状态
**1.通过ps -ef|grep {*program*Name} 找到程序进程ID
[root@10 keep_rule_sender]# ps -ef|grep java
root 4896 18614 0 15:52 pts/0 00:00:00 grep --color=auto java
root 15550 15547 0 5月25 ? 00:24:08 ./jre/bin/java -jar -Dfile.encoding=UTF-8 -XX:+UseG1GC -Xms16384M -Xmx16384M -Xmn8192M -Xss2M AuthCenter.jar

2.top -Hp pid 查看该进程对应的各个线程的运行状态

[root@10 keep_rule_sender]# top Hp 15550
top - 15:55:19 up 115 days, 54 min, 1 user, load average: 0.31, 0.36, 0.40
Threads: 146 total, 0 running, 146 sleeping, 0 stopped, 0 zombie
%Cpu(s): 3.2 us, 0.7 sy, 0.0 ni, 96.1 id, 0.0 wa, 0.0 hi, 0.1 si, 0.0 st
KiB Mem : 13157919+total, 62099732 free, 26188756 used, 43290704 buff/cache
KiB Swap: 4194300 total, 4194300 free, 0 used. 10426378+avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
15937 root 20 0 27.453g 9.252g 13756 S 0.3 7.4 0:16.47 java
15947 root 20 0 27.453g 9.252g 13756 S 0.3 7.4 0:16.73 java

3.jstack pid > stack.txt 保存该程序各个线程当前的执行状态到本地文件stack.txt中

4.查看对应线程的状态

把top看到的线程号(pid)转换成16进制

在stack.txt文件中查看对应线程的执行状态

"nioEventLoopGroup-3-52" #78 prio=10 os_prio=0 tid=0x00007fe8b012c000 nid=0x3e41 runnable [0x00007fe7eb7fa000]
java.lang.Thread.State: RUNNABLE
at sun.nio.ch.EPollArrayWrapper.epollWait(Native Method)
at sun.nio.ch.EPollArrayWrapper.poll(EPollArrayWrapper.java:269)
at sun.nio.ch.EPollSelectorImpl.doSelect(EPollSelectorImpl.java:79)
at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:86)
- locked <0x00000003c0056660> (a io.netty.channel.nio.SelectedSelectionKeySet)
- locked <0x00000003c0056608> (a java.util.Collections$UnmodifiableSet)
- locked <0x00000003c0056618> (a sun.nio.ch.EPollSelectorImpl)
at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:97)
at io.netty.channel.nio.NioEventLoop.select(NioEventLoop.java:639)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:325)
at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:112)
at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:137)
at java.lang.Thread.run(Thread.java:745)

Author: iMine
Link: https://imine141.github.io/2022/08/07/pulsar/%E8%A7%A3%E5%86%B3%E6%96%B9%E6%B3%95/jstack%E5%88%86%E6%9E%90%E7%BA%BF%E7%A8%8B%E8%BF%90%E8%A1%8C%E7%8A%B6%E6%80%81/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.