文档章节

如何定位消耗CPU最多的线程

jepacd
 jepacd
发布于 2017/01/02 15:45
字数 946
阅读 10
收藏 0
for (int i = 0; i < 10; i++) {
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                Thread.sleep(1000000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }, "Thread-" + i).start();
}

Runnable runnable = new Runnable() {
    @Override
    public void run() {
        int i = 0;
        while (true) {
            i = (i++) / 100;
        }
    }
};

Thread busiestThread = new Thread(runnable, "BusiestThread");
busiestThread.start();

上面的代码创建了11个线程。前10个线程什么事儿也不干,只是sleep。而BusiestThread的线程有while死循环。接下来就把这个最耗CPU的线程给揪出来。

  1. 查看进程中线程占用的系统资源

在这一步之前可以使用top命令找出CPU使用率最高的pid,也就是下面的pid。

top -Hp <pid>

输入图片说明

由图可知,pid为9328的线程使用CPU较高

  1. Thread Dump
jstack -l <pid> 
2017-01-02 15:33:55
Full thread dump Java HotSpot(TM) 64-Bit Server VM (24.75-b04 mixed mode):

"Attach Listener" daemon prio=10 tid=0x00007fe938001000 nid=0x24a7 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
	- None

"DestroyJavaVM" prio=10 tid=0x00007fe980008800 nid=0x2455 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
	- None

"BusiestThread" prio=10 tid=0x00007fe9800cd800 nid=0x2470 runnable [0x00007fe97873a000]
   java.lang.Thread.State: RUNNABLE
	at com.iepacj.szjvm.chapter2.BusiestThreadTest$2.run(BusiestThreadTest.java:26)
	at java.lang.Thread.run(Thread.java:745)

   Locked ownable synchronizers:
	- None

"Thread-9" prio=10 tid=0x00007fe9800cb800 nid=0x246f waiting on condition [0x00007fe97883b000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
	at java.lang.Thread.sleep(Native Method)
	at com.iepacj.szjvm.chapter2.BusiestThreadTest$1.run(BusiestThreadTest.java:13)
	at java.lang.Thread.run(Thread.java:745)

   Locked ownable synchronizers:
	- None

"Thread-8" prio=10 tid=0x00007fe9800c9800 nid=0x246e waiting on condition [0x00007fe97893c000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
	at java.lang.Thread.sleep(Native Method)
	at com.iepacj.szjvm.chapter2.BusiestThreadTest$1.run(BusiestThreadTest.java:13)
	at java.lang.Thread.run(Thread.java:745)

   Locked ownable synchronizers:
	- None

"Thread-7" prio=10 tid=0x00007fe9800c7800 nid=0x246d waiting on condition [0x00007fe978a3d000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
	at java.lang.Thread.sleep(Native Method)
	at com.iepacj.szjvm.chapter2.BusiestThreadTest$1.run(BusiestThreadTest.java:13)
	at java.lang.Thread.run(Thread.java:745)

   Locked ownable synchronizers:
	- None

"Thread-6" prio=10 tid=0x00007fe9800c5800 nid=0x246c waiting on condition [0x00007fe978b3e000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
	at java.lang.Thread.sleep(Native Method)
	at com.iepacj.szjvm.chapter2.BusiestThreadTest$1.run(BusiestThreadTest.java:13)
	at java.lang.Thread.run(Thread.java:745)

   Locked ownable synchronizers:
	- None

"Thread-5" prio=10 tid=0x00007fe9800c3800 nid=0x246b waiting on condition [0x00007fe978c3f000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
	at java.lang.Thread.sleep(Native Method)
	at com.iepacj.szjvm.chapter2.BusiestThreadTest$1.run(BusiestThreadTest.java:13)
	at java.lang.Thread.run(Thread.java:745)

   Locked ownable synchronizers:
	- None

"Thread-4" prio=10 tid=0x00007fe9800c1000 nid=0x246a waiting on condition [0x00007fe978d40000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
	at java.lang.Thread.sleep(Native Method)
	at com.iepacj.szjvm.chapter2.BusiestThreadTest$1.run(BusiestThreadTest.java:13)
	at java.lang.Thread.run(Thread.java:745)

   Locked ownable synchronizers:
	- None

"Thread-3" prio=10 tid=0x00007fe9800bf000 nid=0x2469 waiting on condition [0x00007fe978e41000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
	at java.lang.Thread.sleep(Native Method)
	at com.iepacj.szjvm.chapter2.BusiestThreadTest$1.run(BusiestThreadTest.java:13)
	at java.lang.Thread.run(Thread.java:745)

   Locked ownable synchronizers:
	- None

"Thread-2" prio=10 tid=0x00007fe9800bd000 nid=0x2468 waiting on condition [0x00007fe978f42000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
	at java.lang.Thread.sleep(Native Method)
	at com.iepacj.szjvm.chapter2.BusiestThreadTest$1.run(BusiestThreadTest.java:13)
	at java.lang.Thread.run(Thread.java:745)

   Locked ownable synchronizers:
	- None

"Thread-1" prio=10 tid=0x00007fe9800bb000 nid=0x2467 waiting on condition [0x00007fe979043000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
	at java.lang.Thread.sleep(Native Method)
	at com.iepacj.szjvm.chapter2.BusiestThreadTest$1.run(BusiestThreadTest.java:13)
	at java.lang.Thread.run(Thread.java:745)

   Locked ownable synchronizers:
	- None

"Thread-0" prio=10 tid=0x00007fe9800b9000 nid=0x2466 waiting on condition [0x00007fe979144000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
	at java.lang.Thread.sleep(Native Method)
	at com.iepacj.szjvm.chapter2.BusiestThreadTest$1.run(BusiestThreadTest.java:13)
	at java.lang.Thread.run(Thread.java:745)

   Locked ownable synchronizers:
	- None

"Service Thread" daemon prio=10 tid=0x00007fe98009f800 nid=0x2464 runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
	- None

"C2 CompilerThread1" daemon prio=10 tid=0x00007fe98009d000 nid=0x2463 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
	- None

"C2 CompilerThread0" daemon prio=10 tid=0x00007fe98009a000 nid=0x2462 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
	- None

"Signal Dispatcher" daemon prio=10 tid=0x00007fe980098000 nid=0x2461 runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
	- None

"Finalizer" daemon prio=10 tid=0x00007fe980077000 nid=0x2460 in Object.wait() [0x00007fe97974a000]
   java.lang.Thread.State: WAITING (on object monitor)
	at java.lang.Object.wait(Native Method)
	- waiting on <0x00000007d7004858> (a java.lang.ref.ReferenceQueue$Lock)
	at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:135)
	- locked <0x00000007d7004858> (a java.lang.ref.ReferenceQueue$Lock)
	at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:151)
	at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209)

   Locked ownable synchronizers:
	- None

"Reference Handler" daemon prio=10 tid=0x00007fe980075000 nid=0x245f in Object.wait() [0x00007fe953fee000]
   java.lang.Thread.State: WAITING (on object monitor)
	at java.lang.Object.wait(Native Method)
	- waiting on <0x00000007d7004470> (a java.lang.ref.Reference$Lock)
	at java.lang.Object.wait(Object.java:503)
	at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:133)
	- locked <0x00000007d7004470> (a java.lang.ref.Reference$Lock)

   Locked ownable synchronizers:
	- None

"VM Thread" prio=10 tid=0x00007fe980070800 nid=0x245e runnable 

"GC task thread#0 (ParallelGC)" prio=10 tid=0x00007fe98001e000 nid=0x2456 runnable 

"GC task thread#1 (ParallelGC)" prio=10 tid=0x00007fe980020000 nid=0x2457 runnable 

"GC task thread#2 (ParallelGC)" prio=10 tid=0x00007fe980022000 nid=0x2458 runnable 

"GC task thread#3 (ParallelGC)" prio=10 tid=0x00007fe980023800 nid=0x2459 runnable 

"GC task thread#4 (ParallelGC)" prio=10 tid=0x00007fe980025800 nid=0x245a runnable 

"GC task thread#5 (ParallelGC)" prio=10 tid=0x00007fe980027800 nid=0x245b runnable 

"GC task thread#6 (ParallelGC)" prio=10 tid=0x00007fe980029800 nid=0x245c runnable 

"GC task thread#7 (ParallelGC)" prio=10 tid=0x00007fe98002b000 nid=0x245d runnable 

"VM Periodic Task Thread" prio=10 tid=0x00007fe9800aa000 nid=0x2465 waiting on condition 

JNI global references: 108

nid就是线程id的十六进制形式。上步骤找到的9328转换为十六进制就是0x2470。在线程 dump中找到这个线程:

"BusiestThread" prio=10 tid=0x00007fe9800cd800 nid=0x2470 runnable [0x00007fe97873a000]
   java.lang.Thread.State: RUNNABLE
	at com.iepacj.szjvm.chapter2.BusiestThreadTest$2.run(BusiestThreadTest.java:26)
	at java.lang.Thread.run(Thread.java:745)

   Locked ownable synchronizers:
	- None

© 著作权归作者所有

共有 人打赏支持
jepacd
粉丝 5
博文 151
码字总数 210248
作品 0
朝阳
程序员
MySQL运维系列 之 如何快速定位IO瓶颈

MySQL的瓶颈,一般分为IO密集型和CPU密集型 CPU出问题的情况比较少,最近就遇到过一次比较大的故障,这个话题后面会有一篇专题介绍 今天主要聊聊IO密集型的应用中,我们应该如何快速定位到是...

兰春
06/26
0
0
Java线程堆栈分析

不知觉间工作已有一年了,闲下来的时候总会思考下,作为一名Java程序员,不能一直停留在开发业务使用框架上面。老话说得好,机会是留给有准备的人的,因此,开始计划看一些Java底层一点的东西...

ccgogoing
2017/12/12
0
0
[IBM DW] 洞悉 Java 应用性能瓶颈的利器:Visual Performance Anal

简介: 大型 Java 应用调用了大量的类和方法,如何在这成千上万行的代码中找到应用的性能瓶颈呢?在本文中,作者将介绍如何为不同的性能问题选择性能分析工具,对性能问题采 样以及使用 Visu...

红薯
2010/11/07
665
1
Java Trouble Shooting - 使用线程栈

什么是线程栈(thread dump) 线程栈是某个时间点,JVM所有线程的活动状态的一个汇总;通过线程栈,可以查看某个时间点,各个线程正在做什么,通常使用线程栈来定位软件运行时的各种问题,例如...

beanlam
2017/04/21
0
0
从一次线上故障思考Java问题定位思路

原文出处:melonstreet 问题出现:现网CPU飙高,Full GC告警 CGI 服务发布到现网后,现网机器出现了Full GC告警,同时CPU飙高99%。在优先恢复现网服务正常后,开始着手定位Full GC的问题。在...

melonstreet
09/19
0
0

没有更多内容

加载失败,请刷新页面

加载更多

RabbitMq异常处理

1.查找指定文件位置 find . -name "*erlang.cookie" 2.Rabbitmq异常 ===========根据提示,应该是Erlang的cookie出现问题 attempted to contact: ['rabbit@DESKTOP-RVK1IHE'] rabbit@DESKTO......

zhaochaochao
12分钟前
0
0
Java虚拟机学习笔记

jconsole的连接 JConsole的远程连接 JConsole远程连接配置(用VisualVM进行远程连接的配置和JConsole是一摸一样滴) JVM之jconsole远程连接配置...

OSC_fly
12分钟前
0
0
区块链教程以太坊源码分析downloader-peer源码分析

  兄弟连区块链教程以太坊源码分析downloader-peer源码分析,区块链行业正逐渐褪去发展之初的浮躁、回归理性,表面上看相关人才需求与身价似乎正在回落。但事实上,正是初期泡沫的渐退,让...

兄弟连区块链入门教程
14分钟前
0
0
ubuntu 自定义记录用户登录以及操作日志

root@ms:~# cd /etc#更改之前先备份,养成良好的习惯,否则肠子都要悔青了root@ms:/etc# cp profile profile.bakroot@ms:/etc# vi profile 在文章末尾加入下列shell脚本 history US...

Marhal
14分钟前
0
0
liunx 下使用rpm进行软件卸载

rpm -qa | grep php 列出所有的php相关的rpm包 rpm -e xxx 删除包

bengozhong
15分钟前
0
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部