代码:DemoController.java
package com.xxx.demo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* cpu飙升
*
* @author admin
* @date 2021/3/20 10:57
*/
@RestController
@RequestMapping("/api")
public class DemoController {
private volatile boolean flag = true;
@RequestMapping("/a")
public int a(){
flag = true;
int i = 0;
while (flag){
i++;
}
return i;
}
@RequestMapping("/b")
public void b(){
flag = false;
}
}
排查步骤:
1.查看cpu运行
top
找出cpu飙升的进程pid,这里是7711
2.保存堆栈
jstack 7711 > cpu.log
3.找出7711的线程
ps -mp 7711 -o THREAD,tid,time
线程号7730
4.将线程号转为16进制
printf "%x\n" 7730
线程堆对象引用1e32
5.查找线程堆栈
grep cpu.log |grep 1e32 --color -A 20 -B 50
6.定位到代码