2013-05-10 4 views
-1

우리 응용 프로그램이 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); 
     } 
    } 
+2

코드를 살펴보아야합니다. 아니면 우리를 보자. –

+0

코드를 보지 않아도 나는 야생 추측을하는 것 이외에 어떻게 도와 줄 수 있는지 보지 못합니다. – Mat

+0

스레드를 사용량이 많은 루프로 설계했거나 사용하지 않았습니까? 그렇지 않은 경우 버그이며 수정해야합니다. –

답변

4

루프 당신이 스레드에서 잠을 넣어하거나 가능한 한 경쟁을 할 경우, 그것은 단지 CPU

을 사용하고 아니오 "절전"으로 꽉 실행중인 경우 다음 CPU가 아니기 때문에 사용 중

타이머가 루프를 잠자고 있어야하므로 CPU 시간이 덜 걸립니다.

+0

예이 문제가 발생하는 루프가 무한정 실행되고 있습니다. 이것은 우리가 수면을 추가하여 해결할 수 있습니다. 타이머 작업 예약은 CPU주기를 소비하는 새 스레드를 생성합니다. 그러나 타이머 작업이 예약 될 때 CPU 사용량이 어떻게 줄었는지 잘 모르겠습니다. – user2368910

관련 문제