0

How do I limit the CPU usage using this code for my Java game? I think one way might be to use Thread.sleep() but only if I can calculate the time or something like that. Can someone please help me?

public void run(){
    this.requestFocus();
    long lastTime = System.nanoTime();
    double amountOfTicks = 60.0;
    double ns = 1000000000 / amountOfTicks;
    double delta = 0;
    long timer = System.currentTimeMillis();
    int frames = 0;
    while(isRunning){
        long now = System.nanoTime();
        delta += (now - lastTime) / ns;
        lastTime = now;
        while(delta >= 1){
            tick();
            delta--;
        }
        render();
        frames++;

        if(System.currentTimeMillis() - timer > 1000){
            timer += 1000;
            frames = 0;
        }
    }
    stop();     
}
waka
  • 3,362
  • 9
  • 35
  • 54
  • 1
    Could you please point out, why you'd like to limit the CPU-time of your game loop? Maybe you should think about your design. – Paul Kertscher Sep 28 '17 at 05:01
  • What is `tick()` Why don't you do something like `Thread.sleep(delta/1000);` instead of `tick()`, and `delta--;`? – matt Sep 28 '17 at 12:44
  • tick() is my update call. I need to limit my cpu because it uses about 90% for some reason. I think its because I am calling the loop as many times possible. Would thread.sleep work? If so were and for how long? – Evan Hampel Sep 28 '17 at 12:51
  • 1
    Why is it a problem if your program uses 90% CPU? If there's work needs doing, you want the CPU to do it. It's only a problem if you're using the CPU but not getting any useful work done. – slim Sep 28 '17 at 12:53
  • If you're trying to maintain a constant speed on computers with different processor speeds, you should instead calculate how long a loop takes and adjust (e.g.) your sprite's speed accordingly, rather than slow down the program. – Steve Smith Sep 28 '17 at 12:59
  • @SteveSmith do you have a resource on that? – matt Sep 28 '17 at 13:01
  • A quick Google: http://www.java-gaming.org/index.php?topic=24220.0 (Jump to "Variable timestep"). – Steve Smith Sep 28 '17 at 13:13
  • @SteveSmith While this is an option, it's debatable as to whether it would be good in this case. – matt Sep 28 '17 at 13:31

1 Answers1

0

This looks pretty straight forward except for the missing parts. Consider you want to update at some fixed rate.

while(isRunning){


    tick();
    render();
    long now = System.nanoTime();
    double delta = now - lastTime;
    double remaining = delta - ns;
    if(remaining>1000){
        try{
             Thread.sleep((int)(remaining/1000));
        } catch(InterruptedException e){
             throw new RuntimeException(e);
        }
    }

    frames++;
    lastTime = System.nanoTime();
    if(System.currentTimeMillis() - timer > 1000){
        timer += 1000;
        frames = 0;
    }
}
matt
  • 10,892
  • 3
  • 22
  • 34
  • So i should put all of this under: (while running){} – Evan Hampel Sep 28 '17 at 13:27
  • It should pretty much replace your `while(delta>=1)` loop. I can update it with the full `while(isRunning)` loop. – matt Sep 28 '17 at 13:30
  • Okay thank you. So I should keep everything but the delta loop which I will replace with this code? – Evan Hampel Sep 28 '17 at 13:33
  • @EvanHampel I updated it, you could use this to replace your current `while(isRunning)` loop. This will try to fix the rate of execution at 60 hertz, if your computer cannot keep up with that you will still use 90%+ CPU. – matt Sep 28 '17 at 13:37
  • The game is not updating properly now. – Evan Hampel Sep 28 '17 at 22:22
  • @EvanHampel I don't know what "properly" is. You have to describe what is wrong now. The answer I have provided will update 60 times every second, and if there is spare time it will wait. – matt Sep 29 '17 at 05:02
  • my game will now not preform the tick() method. It doesn't it VERY randomly and only once if that. – Evan Hampel Sep 29 '17 at 18:46
  • I had to rename delta and not it works however the game is much faster now and uses the same amount of cpu. – Evan Hampel Sep 29 '17 at 18:51