Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Java
  3. Question
Java

How efficient is the java game loop?

Asked by Roger Lum Oct 13, 2022 1.2K views 2 answers
Share

About this question

 I'm quite new to java.

I was watching 2 video tutorials on how to make a java game and read a couple articles about game loops. However, I'm still confused about how the game loop actually works.

The first tutorial used code that looked like this (I modified some codes from the tutorial and commented on top):

/*
 * Game Loop of the game. It uses a fixed timestep. 
 * Optimal time represents time needed to update one frame.
 * Update time represents time actually taken to update one frame.
 * 
 * By calculating the difference between optimal and actual time,
 * we can let Thread to sleep for the exact time we are aiming for, which is 60 FPS.
 */


public void run() {

    long now;

    long updateTime;

    long wait;


    final int TARGET_FPS = 60;
    final long OPTIMAL_TIME = 1000000000 / TARGET_FPS;
    while (isRunning) {
        now = System.nanoTime();
        update();
        render();
        updateTime = System.nanoTime() - now;
        wait = (OPTIMAL_TIME - updateTime) / 1000000;
        try {
            Thread.sleep(wait);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

}

This is quite easy to understand and I have no problem with using this game loop. However, the second tutorial used more complex game loop that I had a hard time understanding it:


public void run() {
    long startTime = System.nanoTime();
    double amountOfTicks = 60.0;
    double ns = 1000000000 / amountOfTicks;
    double delta = 0;
    long timer = System.currentTimeMillis();
    int frames = 0 ;
    while (running) {
        long now = System.nanoTime();
        delta += (now - lastTime) / ns;
        lastTime = now;
        while (delta >= 1) {
            tick();
            delta--;
        }
        if(running)
            render();
        frames++;
        if(System.currentTimeMillis() - timer > 1000) {
            timer += 1000;
            System.out.println("FPS: " + frames);
            frames = 0;
        }
    }
    stop();
}

I do not understand the use of timer and frames variable and how delta is being used. Is timer and frames simply used for printing out the FPS on the console screen? Also, help me clarify the use of the delta variable in this code.


The use of delta variables is quite different from the other game loop that I know of. From the articles that I read, I've seen a different use of delta. Here's what I tried to come up with on my own after reading the articles:


/*
 * Delta is the ratio of how fast a computer is running. 
 * It is achieved by calculating: 
 * (actual time) / (optimal time) 
 * 
 * This is passed to update method to calculate time taken
 */
public void run() {
    long lastLoopTime = System.nanoTime();
    final int TARGET_FPS = 60;
    final long OPTIMAL_TIME = 1000000000 / TARGET_FPS;
    while (isRunning) {
        long now = System.nanoTime();
        long updateTime = now - lastLoopTime;
        lastLoopTime = now;
        double delta = updateTime / (double) OPTIMAL_TIME;
        update(delta);
        render();
    }
}

Will the above code be fine as it is?

Out of these three sets of codes, which game loop would be the most optimal to use? I feel like the first set of code is too simple that it might not be as efficient as the other two (not that it actually is, but just a feeling that I get).

Your answer

2 Answers

Aethon Latest answer

Answered on May 26, 2026

The efficiency of a Java game loop really depends on how well it's implemented. I was actually stuck on my own timing code for days, making it way too complex, and finally had to take a break just to clear my head. While I was stepping away, I was trying to sort through some frustrating withdrawal delays I had with a gaming site, and using https://lgl.io/ Fast Payout Casinos really helped me clear up the confusion and find a platform that actually worked as promised. Funny enough, fixing that random headache gave me the mental reset I needed, and when I looked at my Java loop again, I realized I just needed to simplify my thread management.

Was this helpful?

More Java discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Java Blogs

Guides, tips and career advice on Java from JanBask experts.