Jump to content
  • Advertisement
Sign in to follow this  
Zaxx

[java] Sleep time in game loop in animation thread

This topic is 4850 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This might go into Game Programming since it deals with game loops, but I'm posting here since it deals some with specific Java classes. For my java game I've been using an extended Panel class with the Runnable interface for my animation, which I got from the web site Killer Game Programming in Java (http://fivedots.coe.psu.ac.th/~ad/jg/). Updating and rendering are separate, though the FPS rate is constrained. I've been thinking of getting rid of the set FPS and update the game state based on actual number of seconds. One thing though that I've been wondering is this sleep() call I make in my main game loop; every time I update/render I have the thread sleep a bit (my loop is in my thread's run() method). What the web site I brought up did was calculate the sleep time required based on how long the loop iteration went and on the default FPS. Now, if I didn't constrain the FPS, I'd have to choose an arbitrary sleep time, would I? So, how do I choose a good sleep time, or does it matter that much? Thank you.

Share this post


Link to post
Share on other sites
Advertisement
The most important thing to consider about sleeping, is that it is not performing a sleep for the amount of time you specified. Rather, it promises not to sleep for LESS then the time specified. I have seen many cases where a Thread.sleep(1) waits a full 15 milliseconds to return.

I just decided to get rid of all sleeping entirely in my game loop, and run at max fps. If this isn't viable for you, you might look in to enabling vertical-sync (or video-sync). Might not be possible to do that in awt/swing though.

Share this post


Link to post
Share on other sites
If you have more than one thread, you may find that one thread will busy-spin for a while before the other gets scheduled. There's no guarantee for the granularity of parallelism. To help out with this, you can yield each of the threads once per iteration, which will help give time to the other threads. If there's not a yield method, sleep(0) usually does the same thing.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!