Java:CPU占用分析
情况
今天发现我服务器上的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,满速跑的!