thread

thread在线教程

查看当前线程信息,查看线程的堆栈

参数说明

参数名称参数说明
id线程id
[n:]指定最忙的前N个线程并打印堆栈
[b]找出当前阻塞其他线程的线程
[i <value>]指定cpu使用率统计的采样间隔,单位为毫秒,默认值为200
[—all]显示所有匹配的线程

cpu使用率是如何统计出来的?

这里的cpu使用率与linux 命令top -H -p <pid> 的线程%CPU类似,一段采样间隔时间内,当前JVM里各个线程的增量cpu时间与采样间隔时间的比例。

工作原理说明:

  • 首先第一次采样,获取所有线程的CPU时间(调用的是java.lang.management.ThreadMXBean#getThreadCpuTime()sun.management.HotspotThreadMBean.getInternalThreadCpuTimes()接口)

  • 然后睡眠等待一个间隔时间(默认为200ms,可以通过-i指定间隔时间)

  • 再次第二次采样,获取所有线程的CPU时间,对比两次采样数据,计算出每个线程的增量CPU时间

  • 线程CPU使用率 = 线程增量CPU时间 / 采样间隔时间 * 100%

注意: 这个统计也会产生一定的开销(JDK这个接口本身开销比较大),因此会看到as的线程占用一定的百分比,为了降低统计自身的开销带来的影响,可以把采样间隔拉长一些,比如5000毫秒。

另外一种查看Java进程的线程cpu使用率方法:可以使用show-busy-java-threads这个脚本

使用参考

支持一键展示当前最忙的前N个线程并打印堆栈:

  1. $ thread -n 3
  2. "C1 CompilerThread0" [Internal] cpuUsage=1.63% deltaTime=3ms time=1170ms
  3. "arthas-command-execute" Id=23 cpuUsage=0.11% deltaTime=0ms time=401ms RUNNABLE
  4. at java.management@11.0.7/sun.management.ThreadImpl.dumpThreads0(Native Method)
  5. at java.management@11.0.7/sun.management.ThreadImpl.getThreadInfo(ThreadImpl.java:466)
  6. at com.taobao.arthas.core.command.monitor200.ThreadCommand.processTopBusyThreads(ThreadCommand.java:199)
  7. at com.taobao.arthas.core.command.monitor200.ThreadCommand.process(ThreadCommand.java:122)
  8. at com.taobao.arthas.core.shell.command.impl.AnnotatedCommandImpl.process(AnnotatedCommandImpl.java:82)
  9. at com.taobao.arthas.core.shell.command.impl.AnnotatedCommandImpl.access$100(AnnotatedCommandImpl.java:18)
  10. at com.taobao.arthas.core.shell.command.impl.AnnotatedCommandImpl$ProcessHandler.handle(AnnotatedCommandImpl.java:111)
  11. at com.taobao.arthas.core.shell.command.impl.AnnotatedCommandImpl$ProcessHandler.handle(AnnotatedCommandImpl.java:108)
  12. at com.taobao.arthas.core.shell.system.impl.ProcessImpl$CommandProcessTask.run(ProcessImpl.java:385)
  13. at java.base@11.0.7/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
  14. at java.base@11.0.7/java.util.concurrent.FutureTask.run(FutureTask.java:264)
  15. at java.base@11.0.7/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304)
  16. at java.base@11.0.7/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
  17. at java.base@11.0.7/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
  18. at java.base@11.0.7/java.lang.Thread.run(Thread.java:834)
  19. "VM Periodic Task Thread" [Internal] cpuUsage=0.07% deltaTime=0ms time=584ms
  • 没有线程ID,包含[Internal]表示为JVM内部线程,参考dashboard命令的介绍。

  • cpuUsage为采样间隔时间内线程的CPU使用率,与dashboard命令的数据一致。

  • deltaTime为采样间隔时间内线程的增量CPU时间,小于1ms时被取整显示为0ms。

  • time 线程运行总CPU时间。

注意:线程栈为第二采样结束时获取,不能表明采样间隔时间内该线程都是在处理相同的任务。建议间隔时间不要太长,可能间隔时间越大越不准确。 可以根据具体情况尝试指定不同的间隔时间,观察输出结果。

当没有参数时,显示第一页线程的信息

默认按照CPU增量时间降序排列,只显示第一页数据。

  1. $ thread
  2. Threads Total: 33, NEW: 0, RUNNABLE: 9, BLOCKED: 0, WAITING: 3, TIMED_WAITING: 4, TERMINATED: 0, Internal threads: 17
  3. ID NAME GROUP PRIORITY STATE %CPU DELTA_TIME TIME INTERRUPT DAEMON
  4. -1 C2 CompilerThread0 - -1 - 5.06 0.010 0:0.973 false true
  5. -1 C1 CompilerThread0 - -1 - 0.95 0.001 0:0.603 false true
  6. 23 arthas-command-execute system 5 RUNNABLE 0.17 0.000 0:0.226 false true
  7. -1 VM Periodic Task Thread - -1 - 0.05 0.000 0:0.094 false true
  8. -1 Sweeper thread - -1 - 0.04 0.000 0:0.011 false true
  9. -1 G1 Young RemSet Sampling - -1 - 0.02 0.000 0:0.025 false true
  10. 12 Attach Listener system 9 RUNNABLE 0.0 0.000 0:0.022 false true
  11. 11 Common-Cleaner InnocuousThrea 8 TIMED_WAI 0.0 0.000 0:0.000 false true
  12. 3 Finalizer system 8 WAITING 0.0 0.000 0:0.000 false true
  13. 2 Reference Handler system 10 RUNNABLE 0.0 0.000 0:0.000 false true
  14. 4 Signal Dispatcher system 9 RUNNABLE 0.0 0.000 0:0.000 false true
  15. 15 arthas-NettyHttpTelnetBootstra system 5 RUNNABLE 0.0 0.000 0:0.029 false true
  16. 22 arthas-NettyHttpTelnetBootstra system 5 RUNNABLE 0.0 0.000 0:0.196 false true
  17. 24 arthas-NettyHttpTelnetBootstra system 5 RUNNABLE 0.0 0.000 0:0.038 false true
  18. 16 arthas-NettyWebsocketTtyBootst system 5 RUNNABLE 0.0 0.000 0:0.001 false true
  19. 17 arthas-NettyWebsocketTtyBootst system 5 RUNNABLE 0.0 0.000 0:0.001 false true

thread –all, 显示所有匹配的线程

显示所有匹配线程信息,有时需要获取全部JVM的线程数据进行分析。

thread id, 显示指定线程的运行堆栈

  1. $ thread 1
  2. "main" Id=1 WAITING on java.util.concurrent.CountDownLatch$Sync@29fafb28
  3. at sun.misc.Unsafe.park(Native Method)
  4. - waiting on java.util.concurrent.CountDownLatch$Sync@29fafb28
  5. at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
  6. at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:836)
  7. at java.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireSharedInterruptibly(AbstractQueuedSynchronizer.java:997)
  8. at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireSharedInterruptibly(AbstractQueuedSynchronizer.java:1304)
  9. at java.util.concurrent.CountDownLatch.await(CountDownLatch.java:231)

thread -b, 找出当前阻塞其他线程的线程

有时候我们发现应用卡住了, 通常是由于某个线程拿住了某个锁, 并且其他线程都在等待这把锁造成的。 为了排查这类问题, arthas提供了thread -b, 一键找出那个罪魁祸首。

  1. $ thread -b
  2. "http-bio-8080-exec-4" Id=27 TIMED_WAITING
  3. at java.lang.Thread.sleep(Native Method)
  4. at test.arthas.TestThreadBlocking.doGet(TestThreadBlocking.java:22)
  5. - locked java.lang.Object@725be470 <---- but blocks 4 other threads!
  6. at javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
  7. at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
  8. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
  9. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
  10. at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
  11. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
  12. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
  13. at test.filter.TestDurexFilter.doFilter(TestDurexFilter.java:46)
  14. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
  15. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
  16. at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
  17. at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
  18. at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
  19. at com.taobao.tomcat.valves.ContextLoadFilterValve$FilterChainAdapter.doFilter(ContextLoadFilterValve.java:191)
  20. at com.taobao.eagleeye.EagleEyeFilter.doFilter(EagleEyeFilter.java:81)
  21. at com.taobao.tomcat.valves.ContextLoadFilterValve.invoke(ContextLoadFilterValve.java:150)
  22. at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
  23. at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
  24. at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
  25. at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:429)
  26. at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1085)
  27. at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
  28. at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
  29. - locked org.apache.tomcat.util.net.SocketWrapper@7127ee12
  30. at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
  31. at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
  32. at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
  33. at java.lang.Thread.run(Thread.java:745)
  34. Number of locked synchronizers = 1
  35. - java.util.concurrent.ThreadPoolExecutor$Worker@31a6493e

注意, 目前只支持找出synchronized关键字阻塞住的线程, 如果是java.util.concurrent.Lock, 目前还不支持。

thread -i, 指定采样时间间隔

  • thread -i 1000 : 统计最近1000ms内的线程CPU时间。

  • thread -n 3 -i 1000 : 列出1000ms内最忙的3个线程栈

  1. $ thread -n 3 -i 1000
  2. "as-command-execute-daemon" Id=4759 cpuUsage=23% RUNNABLE
  3. at sun.management.ThreadImpl.dumpThreads0(Native Method)
  4. at sun.management.ThreadImpl.getThreadInfo(ThreadImpl.java:440)
  5. at com.taobao.arthas.core.command.monitor200.ThreadCommand.processTopBusyThreads(ThreadCommand.java:133)
  6. at com.taobao.arthas.core.command.monitor200.ThreadCommand.process(ThreadCommand.java:79)
  7. at com.taobao.arthas.core.shell.command.impl.AnnotatedCommandImpl.process(AnnotatedCommandImpl.java:96)
  8. at com.taobao.arthas.core.shell.command.impl.AnnotatedCommandImpl.access$100(AnnotatedCommandImpl.java:27)
  9. at com.taobao.arthas.core.shell.command.impl.AnnotatedCommandImpl$ProcessHandler.handle(AnnotatedCommandImpl.java:125)
  10. at com.taobao.arthas.core.shell.command.impl.AnnotatedCommandImpl$ProcessHandler.handle(AnnotatedCommandImpl.java:122)
  11. at com.taobao.arthas.core.shell.system.impl.ProcessImpl$CommandProcessTask.run(ProcessImpl.java:332)
  12. at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
  13. at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
  14. at java.lang.Thread.run(Thread.java:756)
  15. Number of locked synchronizers = 1
  16. - java.util.concurrent.ThreadPoolExecutor$Worker@546aeec1
  17. ...

thread –state ,查看指定状态的线程

  1. [arthas@28114]$ thread --state WAITING
  2. Threads Total: 16, NEW: 0, RUNNABLE: 9, BLOCKED: 0, WAITING: 3, TIMED_WAITING: 4, TERMINATED: 0
  3. ID NAME GROUP PRIORITY STATE %CPU DELTA_TIME TIME INTERRUPTE DAEMON
  4. 3 Finalizer system 8 WAITING 0.0 0.000 0:0.000 false true
  5. 20 arthas-UserStat system 9 WAITING 0.0 0.000 0:0.001 false true
  6. 14 arthas-timer system 9 WAITING 0.0 0.000 0:0.000 false true