if (preferredTicksPerFrame >= 0) { frames++; frameDurationTicks = timer.getTime() - frameStartTick; while (frameDurationTicks < preferredTicksPerFrame) { long sleepTime = ((preferredTicksPerFrame - frameDurationTicks) * 1000) / timer.getResolution(); try { Thread.sleep(sleepTime); } catch(InterruptedException exc) { LoggingSystem.getLogger().log(Level.SEVERE, "Interrupted while sleeping in fixed-framerate", exc); } frameDurationTicks = timer.getTime() - frameStartTick; } if (frames == Long.MAX_VALUE) frames = 0; }
    public void run() {        initSystem();        assertDisplayCreated();        initGame();        if (type == GameType.GRAPHICAL) {            timer = Timer.getTimer();        } else if (type == GameType.HEADLESS) {            timer = new NanoTimer();        }                // Main game loop        try {            float tpf;            started = true;            while ((!finished) && (!display.isClosing())) {                timer.update();                tpf = timer.getTimePerFrame();                                if (type == GameType.GRAPHICAL) {                    InputSystem.update();                }                update(tpf);                render(tpf);                display.getRenderer().displayBackBuffer();                Thread.yield();            }            started = false;        } catch(Throwable t) {            LoggingSystem.getLogger().log(Level.SEVERE, "Main game loop broken by uncaught exception", t);        }        cleanup();        quit();    }