我们注意到我们的应用程序在 Linux 中消耗了 99% 的 CPU 周期,并且我们发现一个线程在无限循环中运行,这导致了这个问题。我们注意到了一个奇怪的行为。该线程根据参数调度一个定时器任务。如果定时任务被安排,CPU 使用率下降到 20%。如果不是预定的 CPU 使用率是 100%。只是想知道如何引入另一个处理线程将 CPU 使用率降低到 10-20%。
public void run()
{
log.info("Starting VCMG Channel Thread...");
while (true) {
if (readPacket()) {
LoyaltyMessageHandle mh = null;
synchronized(this)
{
if(map.containsKey(respSTAN))
{
mh = (LoyaltyMessageHandle) map.get(respSTAN);
mh.setLoyaltyResp(loyaltyObject);
resetHeartBeatTimer();
}
else
{
//Just drop the packet on the floor... It probably timedout.
if (!log.isDebugEnabled())
{
log.warn("Packet: [" + new String(loyaltyObject).substring(0,28) +
"...] DROPPED !!!!!");
}
else
{
log.debug("Packet: [" + new String(loyaltyObject) + "] DROPPED !!!!!");
}
}
}
if(mh != null) {
synchronized(mh) {
mh.notify();
}
}
}
}
}
public synchronized void resetHeartBeatTimer()
{
if (heartBeatTimer != null)
{
heartBeatTimer.cancel();
}
startHeartBeat();
}
public synchronized void startHeartBeat()
{
heartBeatTimeOut = loyaltyMgr.getHeartBeatInactiveTimer() * 1000;
// Timeout value zero indicates that the 'heartbeat' needs to be disabled.
// If the timeout value is less than zero that should be ignored since that will cause exception.
if ((heartBeatTimeOut > 0) && (this.clientSocket.isConnected()))
{
if (heartBeatTimeOut < HEART_BEAT_LOWEST_TIMEOUT)
{
heartBeatTimeOut = HEART_BEAT_LOWEST_TIMEOUT;
}
heartBeatTimer = new HeartBeatTimer(this, loyaltyMgr.getHeartbeatTimeout());
Timer timer = new Timer();
timer.schedule(heartBeatTimer, heartBeatTimeOut);
}
}