Thursday 15 March 2012

java - Locking game update speed causing high CPU usage. How do I make thread sleep when not doing anything? -



java - Locking game update speed causing high CPU usage. How do I make thread sleep when not doing anything? -

so i'm learning how this, , said code inefficient because thread still running when nil updating. when @ cpu usage in task manager, shoots 35 - 45% , 20 in cpu column when black screen beingness rendered. there way create thread sleep when cpu isn't updating anything?

thanks!

public void run() { long lasttime = system.nanotime(); long timer = system.currenttimemillis(); final double ns = 1000000000.0 / 60.0; double delta = 0; int frames = 0; int updates = 0; while (running) { long = system.nanotime(); delta += (now - lasttime) / ns; lasttime = now; while(delta >= 1) { update(); updates++; delta--; } render(); frames++; if(system.currenttimemillis() - timer > 1000) { timer += 1000; updates = 0; frames = 0; } } stop(); }

the way of doing it, execute yourthread.wait() when game stops doing stuff, , yourthread.notify() when game makes action again.

java

No comments:

Post a Comment