Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Why does this basic ticking class use up so much cpu?

Details: For a lot the programs that I develop I use this code (or some slight variant) to "tick" a method every so often, set to the varaible tps (if set to 32 it calls the method tick 32 times every second). Its very essential so I can't remove it from my code as animations and various other parts will break.

Unfortunately it seems to use a sizable amount of cpu usage for a reason I can't figure out. A while back I was thinking about using thread.sleep() to fix this issue but according to this post; it's rather innacurate which makes it unfeasible as this requires reasonably accurate timing.

It doesn't use that much cpu, around 6-11% cpu for a ryzen 1700 in my admittedly short testing, but it's still quite a lot considering how little it's doing. Is there a less cpu intensive method of completing this? Or will the timing be to innacurate for regular usage.

public class ThreadTest {

    public ThreadTest() {
        int tps = 32;

        boolean threadShouldRun = true;
        long lastTime = System.nanoTime();
        double ns = 1000000000 / tps;
        double delta = 0;
        long now;

        while (threadShouldRun) {
            now = System.nanoTime();

            delta += (now - lastTime) / ns;
            lastTime = now;

            while ((delta >= 1) && (threadShouldRun)) {
                tick();
                delta--;
            }
        }
    }

    public void tick() {

    }

    public static void main(String[] args) {
        new ThreadTest();
    }
}

Basic summary: The code above uses 6-11% cpu with a ryzen 1700, is there a way in java to accomplish the same code with less cpu usage and keeping reasonable timing when executing code a certain amount of times per second.


1 Answers

One easy alternative that shouldn't use as much CPU is to use a ScheduledExecutorService. For example:

public static void main(String[] args) {
    ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();

    executor.scheduleAtFixedRate(() -> {

    }, 0, 31250, TimeUnit.MICROSECONDS);
}

Note that 31250 represents the value of 1/32 seconds converted to microseconds, as that parameter accepts a long.

like image 83
Jacob G. Avatar answered Jun 08 '26 22:06

Jacob G.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!