如何定位消耗CPU最多的线程
博客专区 > jEpac 的博客 > 博客详情
如何定位消耗CPU最多的线程
jEpac 发表于11个月前
如何定位消耗CPU最多的线程
  • 发表于 11个月前
  • 阅读 6
  • 收藏 0
  • 点赞 0
  • 评论 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
共有 人打赏支持
粉丝 5
博文 30
码字总数 205996
×
jEpac
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: