高CPU Java应用分析
模拟CPU 40%左右
import java.util.concurrent.CountDownLatch;
public class Main extends Thread {
private CountDownLatch c;
public Main(String name, CountDownLatch c) {
super(name);
this.c = c;
}
@Override
public void run() {
for (int i = 0; i < 500000000; i++) {
System.out.print(i);
}
c.countDown();
}
public static void main(String[] args) throws InterruptedException {
CountDownLatch c = new CountDownLatch(1);
Main thread1 = new Main("thread1", c);
thread1.start();
c.await();
}
}
后台运行jar包
java -jar maven_test.jar &
安装jstack
yum install java-1.8.0-openjdk-devel.x86_64 -y
找到占用CPU最高的进程是Java进程59635
top
找到占用CPU最高的线程59647
top -H -p 59635
输出线程PID 59647对应的16进制数是e8ff
printf "%x\n" 59647
jstack查看Java进程中线程信息,过滤e8ff相关的线程信息
jstack 59635 | grep e8ff -A 20
根据jstack的输出,主流程也是一个线程