-1

私たちのアプリケーションが 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);
        }
    }
4

1 に答える 1