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

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

转载于:https://my.oschina.net/iepac/blog/817388

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值