[FYI] Fixed FrameRate and JRE Thread.sleep() bug

I was suffering a weird problem during this whole week.

To make fixed framerate game, I used Thread.sleep() method call.

For the high-end computers, it worked well.

But for low-end computers, the play speed is almost doubled.





At first, I thought it is caused by duplicated update() call.

But actually, it was a bug of system. (maybe only for windows jre?)



The system clock may run fast when you use the ACPI power management timer as a high-resolution counter on Windows 2000-based, Windows XP-based, and Windows Server 2003-based computers



A workaround can be found here


       new Thread() {
            { this.setDaemon(true); this.start(); }
            public void run() {
                    while(true) {
                        try {
                            Thread.sleep(Integer.MAX_VALUE);
                        }
                        catch(InterruptedException ex) {
                        }
                    }
            }
        };


Hi mulova, using a thread sleep for a fixed frame rate?i think its not a good idea + will lag the game, why dont you multiply things that you want to be fixed by the p/framerate wich is a feature in jme.

Nader said:

Hi mulova, using a thread sleep for a fixed frame rate?i think its not a good idea + will lag the game, why dont you multiply things that you want to be fixed by the p/framerate wich is a feature in jme.

Cause English is not my native language, I can't understand "p/framerate". sorry  :'(
When I saw the class FixedFramerateGame in jme, it used Thread.sleep() to make it fixed.
If you have another option, please show me the way to get fixed framerate other than using Thread.sleep().
Thanks in advance



I think Nader misunderstood something there. Thread.sleep() is actually the safest way to allow a thread (in this case the rendering thread) to be paused so other threads can take precedence for a certain amount of time.

Thanks for sharing that, mulova, that ACPI timer isue on previous generation windows OS's is sadly still unknown to most java developers.