Java应用程序消耗更多的CPU周期

Java应用程序消耗更多的CPU周期,java,Java,我们注意到,我们的应用程序在Linux中消耗了99%的CPU周期,我们发现一个线程在不确定循环中运行,这导致了这个问题。我们注意到这上面有一个奇怪的行为。该线程根据参数安排计时器任务。如果安排了计时器任务,CPU使用率将下降到20%。如果没有计划,CPU使用率为100%。只是想知道引入另一个处理线程如何将CPU使用率降低到10-20% public void run() { log.info("Starting VCMG Channel Thread...");

我们注意到,我们的应用程序在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);
        }
    }
public void run()
{
log.info(“正在启动VCMG通道线程…”);
while(true){
if(readPacket()){
LoyaltyMessageHandle mh=null;
已同步(此)
{
if(集装箱地图(负责人))
{
mh=(忠诚消息句柄)map.get(respSTAN);
mh.setLoyaltyResp(忠诚对象);
重置心跳定时器();
} 
其他的
{
//把包扔在地板上就行了……可能是时候了。
如果(!log.isDebugEnabled())
{
log.warn(“数据包:[”+新字符串(忠诚对象)。子字符串(0,28)+
“…]掉了!!!!!!”;
}
其他的
{
log.debug(“数据包:[“+新字符串(忠诚对象)+“]已删除!!!”;
}
}
}
如果(mh!=null){
同步(mh){
mh.notify();
}
}
} 
}
}
公共同步的void resetHeartBeatTimer()
{
如果(heartBeatTimer!=null)
{
heartBeatTimer.cancel();
}
startHeartBeat();
}
公共同步的void startHeartBeat()
{
heartBeatTimeOut=loyaltyMgr.getHeartBeatInactivativeTimer()*1000;
//超时值为零表示需要禁用“心跳”。
//如果超时值小于零,则应忽略该值,因为这将导致异常。
if((heartBeatTimeOut>0)&&(this.clientSocket.isConnected())
{
if(心跳超时<心跳最低超时)
{
心跳超时=心跳最低超时;
}
heartBeatTimer=新的heartBeatTimer(this,loyaltyMgr.getHeartbeatTimeout());
定时器=新定时器();
时间表(heartBeatTimer,heartBeatTimeOut);
}
}

因为如果循环在没有“睡眠”的情况下紧密运行,那么它只是在使用CPU

如果您在线程中设置了睡眠或以其他方式使争用成为可能,那么CPU就没有被使用


您的计时器必须使循环休眠,因此它使用的CPU时间较少

您应该查看代码。或者让我们来看看。在没有看到代码的情况下,我不知道除了胡乱猜测之外我们还能如何帮助你。要么你设计了忙循环线程,要么你没有。如果您没有,这是一个bug,您应该修复它。是的,这就是正在发生的事情,一个循环正在无限期地运行,这导致了这个问题。我们可以通过增加睡眠来解决这个问题。调度计时器任务会产生一个新线程,该线程也将消耗CPU周期。但我不确定在安排计时器任务时CPU的使用率是如何下降的。