public void run()
    {
        running = true;
        try
        {
            game.setup();
            while (running)
            {
                beforeTime = System.nanoTime();
                skipFramesInExcessTime();
    
                // Updates, renders and paint the screen
                game.processLogics();
                game.renderGraphics();
                game.paintScreen();
                afterTime = System.nanoTime();
    
                long sleepTime = calculateSleepTime();
    
                if (sleepTime >= 0)
                    sleep(sleepTime);
                else
                {
                    excessTime -= sleepTime; // Sleep time is negative
                    overSleepTime = 0L;
                    yieldIfNeed();
                }
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            running = false;
            game.tearDown();
            System.exit(0);
        }
    }