우리 응용 프로그램이 Linux에서 99 % CPU주기를 소비하고 스레드가이 문제를 일으키는 특정 루프에서 실행되고 있음을 확인했습니다. 우리는 이것에 대해 이상한 행동을 발견했습니다. 매개 변수를 기반으로이 스레드는 타이머 작업을 예약합니다. 타이머 작업이 예약 된 경우 CPU 사용량이 20 %로 감소했습니다. 예약되지 않은 경우 CPU 사용량은 100 %입니다. 다른 처리 스레드를 도입하면 CPU 사용량이 10-20 %로 감소하는 것을 알고 싶을 것입니다.Java 응용 프로그램이 더 많은 CPU 사이클을 사용합니다.
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);
}
}
코드를 살펴보아야합니다. 아니면 우리를 보자. –
코드를 보지 않아도 나는 야생 추측을하는 것 이외에 어떻게 도와 줄 수 있는지 보지 못합니다. – Mat
스레드를 사용량이 많은 루프로 설계했거나 사용하지 않았습니까? 그렇지 않은 경우 버그이며 수정해야합니다. –