通过如下命令查找:
ps -mp pid -o THREAD,tid,time
pid是进程ID,换成对应的 PID
ps -mp 2085 -o THREAD,tid,time
USER %CPU PRI SCNT WCHAN USER SYSTEM TID TIME
root 16.7 - - - - - - 5-19:57:24
root 0.0 19 - futex_ - - 2085 00:00:00
root 0.0 19 - poll_s - - 2087 00:00:01
root 0.0 19 - futex_ - - 2088 00:35:37
root 0.0 19 - futex_ - - 2089 00:35:52
root 0.0 19 - futex_ - - 2090 00:35:50
root 0.0 19 - futex_ - - 2091 00:35:41
root 0.0 19 - futex_ - - 2092 00:02:02
root 0.0 19 - futex_ - - 2093 00:20:16
root 0.0 19 - futex_ - - 2094 00:00:26
root 0.0 19 - futex_ - - 2095 00:01:21
root 0.0 19 - futex_ - - 2096 00:00:00
root 0.0 19 - futex_ - - 2097 00:00:00
root 0.0 19 - futex_ - - 2098 00:03:58
root 0.0 19 - futex_ - - 2099 00:03:51
root 0.0 19 - futex_ - - 2100 00:01:01
root 0.0 19 - futex_ - - 2101 00:00:00
root 0.0 19 - futex_ - - 2102 00:45:07
root 0.0 19 - futex_ - - 2103 00:02:38
root 0.0 19 - futex_ - - 2104 00:00:00
root 0.0 19 - ep_pol - - 2105 00:03:02
root 0.0 19 - ep_pol - - 2106 00:02:30
root 0.0 19 - futex_ - - 2110 00:00:34
root 0.0 19 - futex_ - - 2111 00:00:00
root 0.0 19 - futex_ - - 2148 00:04:54
root 0.0 19 - futex_ - - 2149 00:04:46
root 0.3 19 - futex_ - - 2150 02:41:46
root 15.1 19 - ? - - 2160 5-06:43:02
root 0.0 19 - ep_pol - - 2161 00:03:34
root 0.0 19 - ep_pol - - 2162 00:03:35
root 0.0 19 - futex_ - - 2163 00:02:43
root 0.0 19 - inet_c - - 2164 00:02:44
root 0.0 19 - futex_ - - 2165 00:02:12
root 0.0 19 - ep_pol - - 2166 00:02:46
root 0.0 19 - ep_pol - - 2167 00:02:45
root 0.0 19 - inet_c - - 2168 00:00:00
root 0.0 19 - futex_ - - 2169 00:02:09
root 0.0 19 - futex_ - - 2177 00:02:31
root 0.0 19 - futex_ - - 4475 00:00:00
root 0.0 19 - futex_ - - 25924 00:04:44
root 0.0 19 - futex_ - - 9067 00:04:01
root 0.0 19 - futex_ - - 28880 00:03:17
root 0.0 19 - futex_ - - 3034 00:02:20
root 0.0 19 - futex_ - - 19651 00:01:40
root 0.0 19 - skb_re - - 23740 00:00:00
root 0.0 19 - futex_ - - 9455 00:00:41
root 0.0 19 - futex_ - - 9458 00:00:41
root 0.0 19 - futex_ - - 9459 00:00:44
root 0.0 19 - futex_ - - 9461 00:00:37
root 0.0 19 - futex_ - - 28370 00:00:06
图中的 root 15.1 19 - ? - - 2160 5-06:43:02 占用的CPU比较高,有5天6个多小时了。
把线程ID,此处是 2160 转成十六进制
printf "%x\n" tid
printf "%x\n" 2160
870
打印堆栈信息: jstack pid |grep tid -A 1000
jstack 2085 |grep 870 -A 100
"ContainerBackgroundProcessor[StandardEngine[Catalina]]" #24 daemon prio=5 os_prio=0 tid=0x00007f2dc0332800 nid=0x870 waiting on condition [0x00007f2db0050000]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1355)
at java.lang.Thread.run(Thread.java:748)
"TestScheduler_QuartzSchedulerThread" #22 prio=5 os_prio=0 tid=0x00007f2d51768800 nid=0x866 waiting on condition [0x00007f2d88e94000]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at org.quartz.core.QuartzSchedulerThread.run(QuartzSchedulerThread.java:420)
"TestScheduler_Worker-2" #21 prio=4 os_prio=0 tid=0x00007f2d51410000 nid=0x865 in Object.wait() [0x00007f2d88ed5000]
java.lang.Thread.State: TIMED_WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:523)
- locked <0x00000000e1672b80> (a org.quartz.simpl.SimpleThreadPool$WorkerThread)
"TestScheduler_Worker-1" #20 prio=4 os_prio=0 tid=0x00007f2d51415800 nid=0x864 in Object.wait() [0x00007f2da006d000]
java.lang.Thread.State: TIMED_WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:523)
- locked <0x00000000e1672d08> (a org.quartz.simpl.SimpleThreadPool$WorkerThread)
"MySQL Statement Cancellation Timer" #19 daemon prio=5 os_prio=0 tid=0x00007f2d50df0800 nid=0x83f in Object.wait() [0x00007f2da04b8000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000000dfe16c50> (a java.util.TaskQueue)
at java.lang.Object.wait(Object.java:502)
at java.util.TimerThread.mainLoop(Timer.java:526)
- locked <0x00000000dfe16c50> (a java.util.TaskQueue)
at java.util.TimerThread.run(Timer.java:505)
"Tomcat JDBC Pool Cleaner[868693306:1553420517070]" #18 daemon prio=5 os_prio=0 tid=0x00007f2d50d73000 nid=0x83e in Object.wait() [0x00007f2da04f9000]
java.lang.Thread.State: TIMED_WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
at java.util.TimerThread.mainLoop(Timer.java:552)
- locked <0x00000000dfe16c68> (a java.util.TaskQueue)
at java.util.TimerThread.run(Timer.java:505)
"NioBlockingSelector.BlockPoller-2" #15 daemon prio=5 os_prio=0 tid=0x00007f2dc050e800 nid=0x83a runnable [0x00007f2db012e000]
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:93)
at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:86)
- locked <0x00000000df588048> (a sun.nio.ch.Util$3)
- locked <0x00000000df588030> (a java.util.Collections$UnmodifiableSet)
- locked <0x00000000df570900> (a sun.nio.ch.EPollSelectorImpl)
at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:97)
at org.apache.tomcat.util.net.NioBlockingSelector$BlockPoller.run(NioBlockingSelector.java:339)
"NioBlockingSelector.BlockPoller-1" #14 daemon prio=5 os_prio=0 tid=0x00007f2dc04f3000 nid=0x839 runnable [0x00007f2db016f000]
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:93)
at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:86)
- locked <0x00000000df5994c0> (a sun.nio.ch.Util$3)
- locked <0x00000000df5994a8> (a java.util.Collections$UnmodifiableSet)
- locked <0x00000000df570990> (a sun.nio.ch.EPollSelectorImpl)
at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:97)
at org.apache.tomcat.util.net.NioBlockingSelector$BlockPoller.run(NioBlockingSelector.java:339)
"GC Daemon" #13 daemon prio=2 os_prio=0 tid=0x00007f2dc0455800 nid=0x838 in Object.wait() [0x00007f2db01c0000]
java.lang.Thread.State: TIMED_WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000000df5aa7b8> (a sun.misc.GC$LatencyLock)
at sun.misc.GC$Daemon.run(GC.java:117)
- locked <0x00000000df5aa7b8> (a sun.misc.GC$LatencyLock)
"AsyncFileHandlerWriter-225534817" #12 daemon prio=5 os_prio=0 tid=0x00007f2dc0166800 nid=0x837 waiting on condition [0x00007f2dc408a000]
java.lang.Thread.State: TIMED_WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x00000000df599568> (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.LinkedBlockingDeque.pollFirst(LinkedBlockingDeque.java:522)
at java.util.concurrent.LinkedBlockingDeque.poll(LinkedBlockingDeque.java:684)
at org.apache.juli.AsyncFileHandler$LoggerThread.run(AsyncFileHandler.java:144)
"Service Thread" #9 daemon prio=9 os_prio=0 tid=0x00007f2dc0116000 nid=0x835 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C1 CompilerThread2" #8 daemon prio=9 os_prio=0 tid=0x00007f2dc0113000 nid=0x834 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread1" #7 daemon prio=9 os_prio=0 tid=0x00007f2dc0110800 nid=0x833 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread0" #6 daemon prio=9 os_prio=0 tid=0x00007f2dc010e800 nid=0x832 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Signal Dispatcher" #5 daemon prio=9 os_prio=0 tid=0x00007f2dc010c800 nid=0x831 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Surrogate Locker Thread (Concurrent GC)" #4 daemon prio=9 os_prio=0 tid=0x00007f2dc010b000 nid=0x830 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Finalizer" #3 daemon prio=8 os_prio=0 tid=0x00007f2dc00c1800 nid=0x82f in Object.wait() [0x00007f2dc418e000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)