I don’t care if it hogs the CPU as my game’s in the foreground. I gave up worrying about laptop battery life a while back.
I do provide a commandline option to use sleep instead, and also, I use sleep on single-core machines as well.
Your games really are the showcase for Java gaming, and the techniques should probably be “canon” for those of us who want to code silky smooth action games.
Roughly they mean, taking into account the old saying:
yield: I’m willing to give up the rest of my timeslices for any equal or higher priority thread that’s READY (and sometimes in WAIT) to run on this processor in this process.
sleep(n): I’d done with my timeslices, put me in SLEEP state for n+/-g (where g is the granularity of the used timer) then put me in READY for the scheduler. With sleep(0) special cased to go immediately into a READY state.
The reason I ask is 'cause sleep(0) used to be broken on pthreads and I can’t remember the status. And the JVM might be doing something funky under the hood.
In my personal experience using ‘yield’ continuously in a loop hogs the CPU from all other threads on non-Vista/Win7 OS’s, including those for sound and other jobs. This is the main reason why I personally advise against it (although I presume Cas is doing something to mitigate that).
That’s a clever approach, but are you doing that in one loop or in multiple ones? I’d expect if you have more then a couple of threads then you can quickly swamp a multi-core PC too.
In my example I had a thread per playing sound, and it was because of those that I had this issue.
That’s the main loop and the only one that needs to execute glassily smooth; all the other threads in the game use normal thread scheduling techniques. Remote call threads are at lower priority; the sound streaming threads are at higher priority (juddery sound is far more unpleasant than juddery graphics). Dualcore processors of course solve all problems neatly