top發(fā)現(xiàn)java進(jìn)程會(huì)把一個(gè)cpu核打滿
問題定位過程:
1.用top確定把cpu打滿的pid:
命令:top
2.確定哪個(gè)線程用的cpu高
命令:ps -mp 111914 -o THREAD,tid,time
線程id是399
因?yàn)閖ava jstack打印 的線程號是16進(jìn)制,要把10進(jìn)制轉(zhuǎn)換為16進(jìn)制:
命令:printf "%x" 399
結(jié)果:18f
3.用jstack把線程信息輸出到一個(gè)文件里
jstack -l 243 > 555 (243是進(jìn)程id)
4.用16進(jìn)制線程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);
}
這里有個(gè)while循環(huán)尉尾,估計(jì)是沒有激發(fā)wait()條件造成贞瞒。