root@web43:/data/logs/jar# jps -ml
1957 sun.tools.jps.Jps -ml
2021 /data/jar/mall-api-1.0.2-exec.jar
11048 /data/jar/oauth-api-1.0.1-exec.jar
root@web44:~# jps -ml
16721 -- process information unavailable
22226 /data/jar/mall-api-1.0.2-exec.jar
61770 sun.tools.jps.Jps -ml
40235 /data/jar/oauth-api-1.0.1-exec.jar
root@web44:~# jstack 22226
22226: Unable to open socket file: target process not responding or HotSpot VM not loaded
The -F option can be used when the target process is not responding
jstack需要使用与进程一致的用户才能执行。 执行ps -ef查看进程用户
root@web44:~# ps -ef | grep -v grep | grep 22226
www 22226 1 15 Oct30 ? 02:44:02 java -server -jar -Xms2g -Xmx2g -Dspring.profiles.active=prod /data/jar/mall-api-1.0.2-exec.jar
root@web44:~# su - www
www@web44:~$ jstack 22226
2025-10-31 14:16:02
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.151-b12 mixed mode):
"Attach Listener" #15584 daemon prio=9 os_prio=0 tid=0x00007ffb0801b800 nid=0xf54b waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"HikariPool-2 connection adder" #15583 daemon prio=5 os_prio=0 tid=0x00007ffab802a000 nid=0xf528 waiting on condition [0x00007ff94b1f0000]
java.lang.Thread.State: TIMED_WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x0000000082ec8bc0> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:2078)
at java.util.concurrent.LinkedBlockingQueue.poll(LinkedBlockingQueue.java:467)
at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1073)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
"HikariPool-2 connection closer" #15582 daemon prio=5 os_prio=0 tid=0x00007ffab8004000 nid=0xf527 waiting on condition [0x00007ff944083000]
java.lang.Thread.State: TIMED_WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x0000000082ec8ec8> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:2078)
at java.util.concurrent.LinkedBlockingQueue.poll(LinkedBlockingQueue.java:467)
at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1073)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
...
"Service Thread" #17 daemon prio=9 os_prio=0 tid=0x00007ffb6c2d4800 nid=0x56f6 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C1 CompilerThread11" #16 daemon prio=9 os_prio=0 tid=0x00007ffb6c2d1800 nid=0x56f5 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C1 CompilerThread10" #15 daemon prio=9 os_prio=0 tid=0x00007ffb6c2cf800 nid=0x56f4 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C1 CompilerThread9" #14 daemon prio=9 os_prio=0 tid=0x00007ffb6c2cd000 nid=0x56f3 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C1 CompilerThread8" #13 daemon prio=9 os_prio=0 tid=0x00007ffb6c2cb000 nid=0x56f2 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread7" #12 daemon prio=9 os_prio=0 tid=0x00007ffb6c2c9000 nid=0x56f1 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread6" #11 daemon prio=9 os_prio=0 tid=0x00007ffb6c2c7000 nid=0x56f0 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread5" #10 daemon prio=9 os_prio=0 tid=0x00007ffb6c2c5000 nid=0x56ef waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread4" #9 daemon prio=9 os_prio=0 tid=0x00007ffb6c2bb000 nid=0x56ee waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread3" #8 daemon prio=9 os_prio=0 tid=0x00007ffb6c2b9000 nid=0x56ed waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread2" #7 daemon prio=9 os_prio=0 tid=0x00007ffb6c2b7000 nid=0x56ec waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread1" #6 daemon prio=9 os_prio=0 tid=0x00007ffb6c2b5000 nid=0x56eb waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread0" #5 daemon prio=9 os_prio=0 tid=0x00007ffb6c2b2000 nid=0x56ea waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Signal Dispatcher" #4 daemon prio=9 os_prio=0 tid=0x00007ffb6c2b0800 nid=0x56e9 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Finalizer" #3 daemon prio=8 os_prio=0 tid=0x00007ffb6c27e000 nid=0x56e8 in Object.wait() [0x00007ffb38583000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
- locked <0x0000000080021f48> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:164)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209)
"Reference Handler" #2 daemon prio=10 os_prio=0 tid=0x00007ffb6c279800 nid=0x56e7 in Object.wait() [0x00007ffb38684000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:502)
at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
- locked <0x0000000080022118> (a java.lang.ref.Reference$Lock)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)
"VM Thread" os_prio=0 tid=0x00007ffb6c272000 nid=0x56e6 runnable
"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x00007ffb6c020000 nid=0x56d9 runnable
"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x00007ffb6c021800 nid=0x56da runnable
"GC task thread#2 (ParallelGC)" os_prio=0 tid=0x00007ffb6c023800 nid=0x56db runnable
"GC task thread#3 (ParallelGC)" os_prio=0 tid=0x00007ffb6c025000 nid=0x56dc runnable
"GC task thread#4 (ParallelGC)" os_prio=0 tid=0x00007ffb6c027000 nid=0x56dd runnable
"GC task thread#5 (ParallelGC)" os_prio=0 tid=0x00007ffb6c028800 nid=0x56de runnable
"GC task thread#6 (ParallelGC)" os_prio=0 tid=0x00007ffb6c02a800 nid=0x56df runnable
"GC task thread#7 (ParallelGC)" os_prio=0 tid=0x00007ffb6c02c000 nid=0x56e0 runnable
"GC task thread#8 (ParallelGC)" os_prio=0 tid=0x00007ffb6c02e000 nid=0x56e1 runnable
"GC task thread#9 (ParallelGC)" os_prio=0 tid=0x00007ffb6c02f800 nid=0x56e2 runnable
"GC task thread#10 (ParallelGC)" os_prio=0 tid=0x00007ffb6c031800 nid=0x56e3 runnable
"GC task thread#11 (ParallelGC)" os_prio=0 tid=0x00007ffb6c033000 nid=0x56e4 runnable
"GC task thread#12 (ParallelGC)" os_prio=0 tid=0x00007ffb6c035000 nid=0x56e5 runnable
"VM Periodic Task Thread" os_prio=0 tid=0x00007ffb6c2d7800 nid=0x56f7 waiting on condition
JNI global references: 4692
查看对应进程的哪个线程占用CPU过高。
top -H -p PID
www@web44:~$ top -H -p 22226
top - 14:21:35 up 78 days, 10:05, 2 users, load average: 0.78, 0.66, 0.49
Threads: 202 total, 0 running, 202 sleeping, 0 stopped, 0 zombie
%Cpu(s): 5.2 us, 0.5 sy, 0.0 ni, 92.7 id, 1.5 wa, 0.0 hi, 0.1 si, 0.0 st
KiB Mem : 32860616 total, 251892 free, 12971492 used, 19637232 buff/cache
KiB Swap: 4194300 total, 4141308 free, 52992 used. 19234052 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
22384 www 20 0 11.727g 2.808g 136 S 0.0 9.0 3:46.81 java
22327 www 20 0 11.727g 2.808g 136 S 0.0 9.0 2:46.26 java
22328 www 20 0 11.727g 2.808g 136 S 0.0 9.0 2:36.46 java
22246 www 20 0 11.727g 2.808g 136 S 0.0 9.0 0:59.39 java
22263 www 20 0 11.727g 2.808g 136 S 0.0 9.0 0:49.83 java
22245 www 20 0 11.727g 2.808g 136 S 0.0 9.0 0:46.44 java
22244 www 20 0 11.727g 2.808g 136 S 0.0 9.0 0:46.02 java
shift + T 按TIME排序
线程的PID转换为16进制
www@web44:~$ printf "%x\n" 22384
5770
www@web44:~$jstack 22226 | grep 5770
https://github.com/oldratlee/useful-scripts
https://raw.githubusercontent.com/oldratlee/useful-scripts/release-2.x/bin/show-busy-java-threads
# 自动识别Java进程,显示最繁忙的5个线程
./show-busy-java-threads.sh
# 显示最繁忙的10个线程
./show-busy-java-threads.sh -c 10
# 指定Java进程ID
./show-busy-java-threads.sh -p 12345
# 持续监控模式,每3秒刷新一次
./show-busy-java-threads.sh -c 5 -i 3
- 先用show-busy-java-threads.sh快速定位
- 如果问题不明显,再用jstack dump完整线程栈
- 扔到fastthread.io深度分析
使用步骤
1. 生成线程栈文件:
# 获取Java进程ID
jps -l
# dump线程栈(带锁信息)
jstack -l <pid> > thread_dump.txt
Arthas - 阿里开源的诊断神器
# 下载arthas-boot.jar
curl -O https://arthas.aliyun.com/arthas-boot.jar
# 启动并attach到Java进程
java -jar arthas-boot.jar
启动后会列出当前所有Java进程,输入序号选择要诊断的进程:
[INFO] arthas-boot version: 3.7.1 [INFO] Found existing java process, please choose one and input the serial number. * [1]: 12345 com.company.Application [2]: 23456 org.elasticsearch.bootstrap.Elasticsearch
1
查看CPU占用最高的线程
显示CPU占用最高的3个线程
thread -n 3
持续监控某个线程
# 查看指定线程ID的详细信息
thread 156
# 每3秒刷新一次
thread -i 3000
查看所有线程的状态分布
thread -i 3000
https://mp.weixin.qq.com/s/8YdKuAG7MKsdyG7yFecw7w
https://mp.weixin.qq.com/s/VUJ3FtNE2M18HmV1l8Ymmw
https://mp.weixin.qq.com/s/8YdKuAG7MKsdyG7yFecw7w
https://blog.csdn.net/u013467442/article/details/88957485
SundayHK