2019独角兽企业重金招聘Python工程师标准>>>
查找线程PID
1,使用top命令,找到java进程的PID,我这里找到的PID为 13520
定位线程
找到该进程后,就要定位具体线程或代码,首先显示线程列表,并按照CPU占用高的线程排序:
ps -mp 13017 -o THREAD,tid,time | sort -rn
root># ps -mp 13017 -o THREAD,tid,time | sort -rn
USER %CPU PRI SCNT WCHAN USER SYSTEM TID TIME
root 99.9 19 - - - - 13520 14:44:09
root 100 - - - - - - 14:52:22
root 0.6 19 - futex_ - - 13519 00:05:41
root 0.0 19 - skb_wa - - 29473 00:00:00
root 0.0 19 - poll_s - - 13018 00:00:00
root 0.0 19 - inet_c - - 13515 00:00:00
...
线程ID转换为16进制格式
root># printf "%x" 13520
34d0
打印线程的堆栈信息
最后使用jvm命令打印线程的堆栈信息:
jstack 13017 |grep 34d0 -A 30
root># jstack 13017 |grep 34d0 -A 30
"SysLogManager" #74 daemon prio=5 os_prio=0 tid=0x00007f0250239800 nid=0x34d0 runnable [0x00007f022117d000]
java.lang.Thread.State: RUNNABLE
at com.rd.ifaes.common.syslog.SysLogManager.run(SysLogManager.java:84)
"System Clock" #73 daemon prio=5 os_prio=0 tid=0x00007f024c13a800 nid=0x34cf waiting on condition [0x00007f022147e000]
java.lang.Thread.State: TIMED_WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x00000007bbae3e00> (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.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1093)
at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:809)
at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074)
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)
"Thread-10" #72 daemon prio=5 os_prio=0 tid=0x00007f024c0a7800 nid=0x34ce waiting on condition [0x00007f022157f000]
java.lang.Thread.State: TIMED_WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x00000007bbb57948> (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.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1093)
at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:809)
at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074)
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)
"Thread-9" #71 daemon prio=5 os_prio=0 tid=0x00007f025017f800 nid=0x34cd waiting on condition [0x00007f0221680000]
这样就找到占用java,cpu资源的代码了,接下来还得分析代码执行的原因了。