Java程序CPU消耗分析之找出最耗CPU线程

参考:https://zhuanlan.zhihu.com/p/80850972

java程序CPU消耗过高一般有两种情况:

1、 us过高,应用占用CPU资源过高,需找出具体占用CPU的线程所执行的代码,分析定位问题原因。

分析步骤如下:

 

(1) 使用top命令找出占用cpu最高的JAVA进程

 

 

(2) 找出占用cpu最高的线程 
top -Hp 1781

 

(3) 占CPU最高线程17596换算成16进制对应线程44bc
用命令
printf "%x\n" 17596

 

(4) 打印占CPU最高JAVA进程1781的堆栈信息 
jstack 1781> stackdump.txt

 

代码如下:

public class CPUConsumeTest {

    public static void main(String[] args) {

        int count = Runtime.getRuntime().availableProcessors();

        System.out.println("processors " + count);

        for(int i=0; i<count; i++) {

            new Thread(new ConsumeCPUTest()).start();

        }

       

        for(int i=0; i<100; i++) {

            new Thread(new NotConsumeCPUTest()).start();

        }

    }

   

}

class ConsumeCPUTest implements Runnable {

    public void run() {

        while(true) {

            for(int i=0; i<10000000; i++) {

                long l = 1000000;

                Math.acos(l);

            }

            try {

                Thread.currentThread().sleep(20);

            } catch (InterruptedException e) {

                Thread.currentThread().interrupt();

            }

        }

    }

}

class NotConsumeCPUTest implements Runnable {

    public void run() {

        while(true) {

            try {

                Thread.currentThread().sleep(50);

            } catch (InterruptedException e) {

                Thread.currentThread().interrupt();

            }

        }

    }

}

 

 

 

2、sy过高,上下文切换过频繁,将线程栈打印出来,看是否有哪个锁竞争过于激烈

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值