I was working on my game, adding stuff to it, when I noticed my ticks were in the low 50's when I had set it to do a constant 60. This is my run method.

public void run() {
    long lastTime = System.nanoTime();
    final double amountOfTicks = 60.0;
    double ns = 1000000000.0 / amountOfTicks;
    double delta = 0;
    int updates = 0;
    int frames = 0;
    long timer = System.currentTimeMillis();

    while (running){
        long now = System.nanoTime();
        delta += (now - lastTime) / ns;
        lastTime = now;
        if (delta >= 1) {

        try {
        } catch (InterruptedException e) {

        if (System.currentTimeMillis() - timer > 1000) {
            timer += 1000;
            frame.setTitle(title + "   |   " + "Frames Per Second: "
                    + frames);
            System.out.println(updates + " Ticks, " + frames + " Frames");
            updates = 0;
            frames = 0;

I've tried removing all the code I added before this occurrence happened and it didn't help. Suggestions would be great!


2 Answers 2


Delete the Thread.sleep() call (assuming you're on a desktop machine and have power to spare!).

In general you never sleep in your game loop (except on Android, where there appears to be no other choice). The bad thing about Thread.sleep is it is unpredictable and may cause your game to give up exec time for longer than 16ms, which is the maximum time a frame can take to complete a frame at 60 fps.

If there is too much time at the end of your loop, you usually can let the GPU VSYNC keep you at 60 FPS. If VSYNC is off, then you busy wait, basically in the form of a loop like:

while( getTimeDeltaThisFrame() <= 16 ms )  ;  // keep "spinning" until
                                              // 16ms have passed.

Note that busy waiting on a portable device is going to burn a lot of battery, and you may have to allow a sleep call and step down to a lower frame rate there.

  • 1
    \$\begingroup\$ Allow me to append: For desktop programs. On a portable device, you will be burning a lot more battery, so you'd worry about the 100% usage thing. \$\endgroup\$
    – bobobobo
    Commented May 9, 2013 at 2:00
  • 1
    \$\begingroup\$ @bobobobo: Would sticking a Thread.yield() in the busy loop have ill effects? I think it'd be slightly "nicer" than a simple busy loop at least. \$\endgroup\$ Commented Aug 13, 2014 at 0:42

Make sure never to have the Thread.sleep(). It takes time for a render to complete... That's why the FPS is lower than expected.

  • 6
    \$\begingroup\$ Thread.sleep is unpredictable. Calling Thread.sleep is like going to sleep really late and setting your alarm clock really early. You might wake up in time, but you also might "hit snooze" many times and wake up later than you wanted. The O/S may not wake up your game 2ms later. It may be 30 ms later. Once you call Thread.sleep, you lose control of the CPU and relinquish it to the O/S. For that reason you cannot call Thread.sleep in a game loop. \$\endgroup\$
    – bobobobo
    Commented Apr 25, 2013 at 20:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .