Game loop and capping fps

Hi, I currently have a game loop that has a fixed fps because the updates and the rendering are coupled together. I plan on separating the updates and rendering by changing the current loop into something like the following:


private void runGameLoop()
{
    long nextUpdate = System.nanoTime();

    while (isRunning)
    {
        int skipped = 0;

        while (System.nanoTime() > nextUpdate && skipped++ < MAX_FRAME_SKIPS)
        {
            updateState();
            nextUpdate += UPDATE_INTERVAL;
        }

        float offset = (float)(UPDATE_INTERVAL - (nextUpdate - System.nanoTime())) / UPDATE_INTERVAL;
        updateDisplay(offset);
    }
}

My problem with that method is it renders as many frames as possible. I want to be able to control the fps and cap it when I want to, so I’m thinking of changing it further to this:


private void runGameLoop()
{
    long nextStateUpdate = System.nanoTime();
    long nextFrameUpdate = System.nanoTime();

    while (isRunning)
    {
        int skipped = 0;
        long currentTime = System.nanoTime();

        while (currentTime > nextStateUpdate && skipped++ < MAX_FRAME_SKIPS)
        {
            updateState();
            nextStateUpdate += STATE_UPDATE_INTERVAL;
        }

        currentTime = System.nanoTime();

        if (currentTime > nextFrameUpdate)
        {
            float offset = (float)(frameUpdateInterval - (nextFrameUpdate - currentTime)) / frameUpdateInterval;
            updateDisplay(offset);
            nextFrameUpdate += frameUpdateInterval; // i'll be able to control the fps on the fly by changing frameUpdateInterval
        }
    }
}

I like that I’ll be able to control the fps, but the game would constantly be running the loop even when there’s nothing to do. Plus, I feel like I’m overlooking something. My main question is are there better ways to do this? Also, is there a better term for the float (offset) passed in updateDisplay?