Best Java 1.4.2 (Java only) timer.

What is the best possible timer (timing algorithm) I can get using only Java 1.4.2? Thread.sleep()? Busy waiting using Thread.yield()? The last one really slows down the GUI.

Best Regards,

Johan Tibell

Suns version of 1.4.2 has the sun.misc.Perf hires timer.
Couple that with a Thread.yield() loop and the skies the limit.

What would such a solution look like?

nextLoop = getCurrentTime();
while (true)
{
   nextLoop = nextLoop + LOOP_TIME;

   while (getCurrentTime() < nextLoop)
      Thread.yield();
}

I’ve implemented such a loop using System.currentTimeMillis() and it slows down the GUI alot (windowed application).

[quote]it slows down the GUI alot (windowed application).
[/quote]
If you arn’t running fullscreen, and have other things happening besides your animation, you may want to use a variable sleep length approach.
Once you complete a frame, calculate how long until the next frame is needed, and sleep that duration.


nextLoop = getCurrentTime(); //either System.currentTimeMills() [non-windows], System.nanoTime() [1.5] or sun.misc.Perf [Sun 1.4.2]
while(true)
{
   //game code
   nextLoop+=LOOP_TIME;
   currentTime = getCurrentTime();
   if(currentTime < nextLoop)
   {
      Thread.sleep(nextLoop-currentTime);
   }
}

Sleep isn’t that accurate, so you might want to change the above loop so it only sleeps if the sleepPeriod is > 1ms.

Try it out and see.
I can’t be much more use, I don’t do animation+gui games, more work than its worth IMO.

[quote]Suns version of 1.4.2 has the sun.misc.Perf hires timer.
Couple that with a Thread.yield() loop and the skies the limit.
[/quote]
So in the rt.jar (/sun/misc/) its the Perf.class or the Timer.class?

Perf timer = sun.misc.Perf.getPerf();

long timerResolution = timer.highResFrequency();
long time = timer.highResCounter();

[quote]Try it out and see.
I can’t be much more use, I don’t do animation+gui games, more work than its worth IMO.
[/quote]
What do you do then? :slight_smile:
I mean it slows down the GUI in the sense that clicking on the close window button in the top left corner is slow. Keyboard interaction also seams a bit slow.

So long as you have a Thread.yield() in the render loop, there shouldn’t be any issue with slowing down any OS rendering.
Atleast, it doesn’t cause any slowdown on my machine :smiley:

Java3D and JMF also come with timers that should be better than System.currentTimeMillis().

Theres nothing like raising a Thread from the dead, simply to add a trivial, obvious and unimportant fact :-*

Take pity, he’s just a poor blob :slight_smile:

[quote]Java3D and JMF also come with timers that should be better than System.currentTimeMillis().
[/quote]
You could hope, but at least in the case of JMF you will be disappointed.

I must be missing something here, I get:
java.lang.UnsatisfiedLinkError: registerNatives
at sun.misc.Perf.registerNatives(Native Method)
Do i need something in my classpath or librarypath?
// Gregof

what JVM version? and what platform?

Oh well, it was a nice thought… :-[ I guess on Windows then, this has implications for syncing media and stuff??? I suppose if this was problematic one could write a GAGETimeBase class, but I guess the use of SystemTimeBase is probably hard-coded.

[quote]what JVM version? and what platform?
[/quote]
I’m sorry, it was my bad, I didnt notice that I had 2 jdk’s installed and was using the wrong one :frowning:
It works now.
Thanks //
Gregof

I believe you mean that you can write a “NativeTimer” implementation for GAGE that uses the Perf timer. GAGE supports just about every other timer, with the System.currentTimeMillis() being a hard coded fall-back. (BTW, System.currentTimeMillis() will give you 50FPS as long as you use GAGETimer.) I haven’t written a “Perf” timer implementation because it isn’t a supported API. Besides, it would only be helpful for Applets. (I don’t like Applets and wish they would finally die. They really don’t serve any useful purpose anymore, and just cause people to confuse Java with “Slow Applets”.) Applications can just use the Windows DLL.