Java:CPU占用分析:修订间差异
imported>Soleverlee 以“=情况= 今天发现我服务器上的Java Web程序CPU一直占用50%,双核一个核占用满了。于是查找一下原因。 [[Image:Java_Cpus_Top.png|600px]...”为内容创建页面 |
imported>Soleverlee |
||
第22行: | 第22行: | ||
[[Image:Jstack_Find_Result.png|600px]] | [[Image:Jstack_Find_Result.png|600px]] | ||
然后恍然大悟,来看看我的这几行代码: | |||
<source lang="java"> | |||
public class MessageHandler implements Runnable { | |||
private static Logger logger = Logger.getLogger(MessageHandler.class.getName()); | |||
public static final ConcurrentLinkedQueue<InMsg> msgQueue = new ConcurrentLinkedQueue<InMsg>(); | |||
static final ExecutorService pool = Executors.newCachedThreadPool(); | |||
@Override | |||
public void run() { | |||
logger.info("=>Running : MessageHandleJob..."); | |||
InMsg item = null; | |||
while (true) { | |||
item = msgQueue.poll(); | |||
if (item == null) { | |||
continue; | |||
} | |||
if (item instanceof InImageMsg) { | |||
InImageMsg imgMsg = (InImageMsg) item; | |||
logger.info("Pushing job to queue..."); | |||
pool.execute(new SyncImageJob(imgMsg)); | |||
} | |||
} | |||
} | |||
} | |||
</source> | |||
整个线程没有sleep,满速跑的! | |||
[[Category:Programe]] | [[Category:Programe]] |
2015年11月25日 (三) 16:41的最新版本
情况
今天发现我服务器上的Java Web程序CPU一直占用50%,双核一个核占用满了。于是查找一下原因。
查找线程号
找出此进程下的所有线程,然后找出最耗cpu线程号。
top -p 9629 -H
定位线程
jstack 9629 >cpu.log
python
>hex(9629)
>'0x259d'
然后在cpu.log中查找0x259d的内容:
然后恍然大悟,来看看我的这几行代码:
public class MessageHandler implements Runnable {
private static Logger logger = Logger.getLogger(MessageHandler.class.getName());
public static final ConcurrentLinkedQueue<InMsg> msgQueue = new ConcurrentLinkedQueue<InMsg>();
static final ExecutorService pool = Executors.newCachedThreadPool();
@Override
public void run() {
logger.info("=>Running : MessageHandleJob...");
InMsg item = null;
while (true) {
item = msgQueue.poll();
if (item == null) {
continue;
}
if (item instanceof InImageMsg) {
InImageMsg imgMsg = (InImageMsg) item;
logger.info("Pushing job to queue...");
pool.execute(new SyncImageJob(imgMsg));
}
}
}
}
整个线程没有sleep,满速跑的!