top发现java进程会把一个cpu核打满
问题定位过程:
1.用top确定把cpu打满的pid:
命令:top
2.确定哪个线程用的cpu高
命令:ps -mp 111914 -o THREAD,tid,time
线程id是399
因为java jstack打印 的线程号是16进制,要把10进制转换为16进制:
命令:printf "%x" 399
结果:18f
3.用jstack把线程信息输出到一个文件里
jstack -l 243 > 555 (243是进程id)
4.用16进制线程id号搜索线程信息文件:
"raft-server-apply-worker-" #37 prio=5 os_prio=0 tid=0x00007fb3cd16d000 nid=0x18f runnable [0x00007fb1cc4d6000]
java.lang.Thread.State: RUNNABLE
at com.jcloud.jcq.raft.server.RaftApplyService.run(RaftApplyService.java:52)
Locked ownable synchronizers:
- None
5.定位代码:
public void run() {
while (!Thread.currentThread().isInterrupted()){
try{
long applyIndex = state.getLastAppliedIndex() + 1;
if (applyIndex > commitIndex) {
continue;
}
if (applyIndex == commitIndex) {
synchronized (this){
wait();
}
}
try{
while (applyIndex <= commitIndex) {
beforeCalls.apply(UUID.randomUUID().toString(), applyIndex);
++applyIndex;
}
}catch (Exception e){
logger.info("when apply at {} may hasException, ignore it", applyIndex);
}
这里有个while循环,估计是没有激发wait()条件造成。