[java] Delay in Java

Started by
4 comments, last by H_o_p_s 19 years ago
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!!
Advertisement
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)
I can't remember off the top of my head, but look up the Timer object. It can be used to delay animations and such.
don;'t user Timer, it sucks (I've used it a lot). It's not fully implemented at the API level, and so it works so badly that it'll take you less time to do everything yourself.
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!!
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)

This topic is closed to new replies.

Advertisement