Sign in to follow this  
kino

[java] Delay in Java

Recommended Posts

kino    122
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!

Share this post


Link to post
Share on other sites
H_o_p_s    877
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().

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
kino    122
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 !!

Share this post


Link to post
Share on other sites
H_o_p_s    877
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this