Skip to main content
gamedev.net
🔒 Locked

[java] Delay in Java

Started by kino Apr 13, 2005 at 3:21 PM 4 replies 3.9k views
Original Post
kino
kino
Is there anything like the delay function in Java? I'm using this: try { Thread.sleep(long milliseconds); } catch(InterruptedException ie) { } Is there any other option? Thanks! Bye Bye!
Long live Rock!!
H_o_p_s
H_o_p_s
Thats the one that I use.... But you have to keep in mind that using Thread.sleep(); will make sure that it sleeps at least the long milliseconds. This means that if you want it to sleep 5 seconds, it will sleep five or more seconds.

If you want the thread to yeild to other threads (like in the main loop) use Thread.yeild().
BRING BACK THE BLACK (or at least something darker)
kino
kino
Quote:
will make sure that it sleeps at least the long milliseconds


Didn’t know that about thread.sleep()… But how far goes this lack of precision, I mean, if I want the thread to sleep 1 second, 1000 milliseconds, would it keep sleeping until how much time?

Quote:
… look up the Timer object


It’s nice to know other ways to do something, and so is to get opinions from people who already tried them…

Thanks all !!
Long live Rock!!
H_o_p_s
H_o_p_s
Thread.sleep() is pretty acurate at higher values. If your using it in your game loop, and it needs to sleep ten milliseconds or less, it will be as much as five milliseconds or more off. This is caused by the different implementations of the internal system timer on different JRE on the different platforms.

I believe that XP, Linux, and MacOSX all have 1ms internal timers for the JRE, but when you get into some of the older Windows operating systems like Windows98 the system timer can be as bad as only updating every 50ms. This makes it so when you say Thread.sleep(1) it actually sleeps 50.

But all in all it is quite usable. I'm using it in my game which runs ~100fps and it doesn't make any messups (mind you I'm on OSX), but if your planning on having it wait something over than the 50ms your fins on any platform. 1 second sleeps should have no problem at all.
BRING BACK THE BLACK (or at least something darker)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.