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

How efficient is the java game loop?

Asked by Al German Oct 6, 2022 2.0K views 4 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

4 Answers

Dave Ginger Latest answer

Answered on Jun 29, 2026

Jeśli polujesz na bonus powitalny, warto sprawdzić https://slottywayscasino.com/bonusy/. Znalazłem tam aktualne informacje bez przekopywania kilku różnych stron. Oszczędziło mi to trochę czasu.

Was this helpful?

Kaelandria

Answered on May 13, 2026

For seasoned gamblers like myself who have navigated the tumultuous seas of online betting for years, Fast Payout Casinos emerge as a revelation that's reshaping our experience. These platforms stand out in the cluttered casino landscape, offering something we've craved for ages – speed and reliability in cash-outs. No longer shackled by endless waiting periods and opaque processing times, I now enjoy seamless withdrawals that reflect instantly without the frustrating lag. This efficiency speaks volumes about how these https://nflupdate.co/ casinos prioritize user satisfaction over cumbersome bureaucracy. Their commitment to advanced financial technology and robust security protocols ensures my winnings are accessible almost immediately, obliterating any anxiety surrounding potential delays or losses. Today, Fast Payout Casinos are not just an option but a necessity for serious players who value their time and anticipate prompt returns on their hard-earned stakes.

Was this helpful?

Amandalawrancee

Answered on Mar 16, 2026

i ran into the same confusion about “delta” meaning two different things depending on the tutorial, and your breakdown is pretty close. in the second loop delta is just an accumulator so tick runs at 60hz even when render runs faster or slower, while timer and frames are only there to print an fps counter. in your third version, passing delta to update is fine, but without clamping or an accumulator you can get weird physics on slow frames. i also wish tutorials stayed on topic; one i watched even had random casino links such as https://red-door-roulette.com/ https://livemegaball.com/ and https://sweetbonanzashow.org/ shoved into the description, which made it harder to trust the code. for most small java games i prefer the fixed-update variable-render style from the second loop, plus interpolation if you want smoothness.

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.