1. 模拟场景
/**
* 模拟 cpu 飙升场景
* @author Maynard.ran
*/
@Component
public class CpuReaper {
@PostConstruct
public void cpuReaper() {
int num = 0;
long start = System.currentTimeMillis() / 1000;
while (true) {
num = num + 1;
if (num == Integer.MAX_VALUE) {
System.out.println("reset");
num = 0;
}
if ((System.currentTimeMillis() / 1000) - start > 1000) {
return;
}
}
}
}
2.定位出问题的线程
top 定位线程最高进程
执行top命令,查看所有进程占系统CPU的排序,定位是哪个进程搞的鬼。在本例中就是咱们的java进程。PID那一列就是进程号
top -Hp pid 定位使用 CPU 最高的线程
3.printf '0x%x' tid 线程 id 转化 16 进制
printf '0x%x' 12817
0x3212
4.jstack pid | grep tid 找到线程堆栈
定位到问题去解决