1、用top定位哪个进程对cpu的占用过高
2、ps H -eo pid,tid,%cpu | grep 进程id (用ps命令进一步定位是哪个线程引起的cpu占用过高)
3、jstack 进程id 可以根据线程id 找到有问题的线程,进一步定位到问题代码的源码行号
4、根据有问题的线程编号 定位到具体哪一个线程出现了问题,记得把线程的编号转换成16进制的编号(程序员计算器),然后确定是那个代码出现了问题
举例 有一个线程超负荷运行 javac编译 然后 nohup java Demo & 后台执行
/**
* 演示 cpu 占用过高
*/
public class Demo {
public static void main(String[] args) {
new Thread(null, () -> {
System.out.println("1...");
while(true) {
}
}, "thread1").start();
new Thread(null, () -> {
System.out.println("2...");
try {
Thread.sleep(1000000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}, "thread2").start();
new Thread(null, () -> {
System.out.println("3...");
try {
Thread.sleep(1000000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}, "thread3").start();
}
}