Java application consumes more CPU cycles -
we noticed our application consumes 99% cpu cycle in linux , identified thread running in indefinite loop, causes issue. noticed strange behavior on this. based on parameters, thread schedule timer task. if timer task scheduled cpu usage went down 20%. if it's not scheduled cpu usage 100%. curious know how introducing processing thread brings down cpu usage 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 packet on floor... 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 0 indicates 'heartbeat' needs disabled. // if timeout value less 0 should ignored since 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); } }
because if loop running tight no "sleep" using cpu
if put sleep in or otherwise make contention possible thread cpu isn't being used
your timer must sleeping loop , using less cpu time
Comments
Post a Comment