123
-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|522|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
Socoder -> On Topic -> Time in Java

Sun, 15 Jul 2012, 15:38
jedimastersterli
I know most of you guys do something with game writing. I'm writing a physics simulation to demonstrate AI, but there isn't much difference there. Games use governing rules to determine outcomes and AIs to work as characters.

I have no problem writing the physics, but time is a problem. I want to make the simulator give video feed rather than just run the program and give the outcome. Does anyone know how to tell Java to wait say .05 seconds between frame changes so that I can get a smooth video?
Sun, 15 Jul 2012, 16:11
JL235
So you just want to sleep for half a second? You can use the sleep method in the Thread class to sleep the current thread.



However sleep will only sleep for an approximate amount of time, not the exact amount given. Depending on your needs, this might be acceptable, and in an ideal world this is the best strategy to use. Half a second is a very long time for a computer, so it should be accurate enough.

If that doesn't work for you, an alternative to sleeping is to just spin, wasting CPU cycles, until enough time has passed. This gives a lot more accuracy, but shouldn't be used for long duration. It also has the downside that the CPU will be using full load during this time, and so will affect other threads on single or dual core machines.

Sometimes this is acceptable, for example Half Life 2 uses this strategy internally in a few places. There are also some commercial games written in Java which just spin when they have no work to do.



You can also try mixing the two, so you sleep for long periods, and then use a spin lock for the last short duration. For example:



Note that I've not tested the above code, that's just a general idea on a couple of approaches. I'd advise playing around with those, depending on your needs.

Even today, sleeping is still a hard problem.
Sun, 15 Jul 2012, 20:59
jedimastersterli
That helps enormously. For my current purposes that works just fine. However as my simulations and AI begin taking up more time they could take up as much time as the sleep. Assuming that calculations take up an appreciable amount of the 20th of a second I want between frames is there a way to make the program work to completion and then wait the remainder of the sleep time before displaying the next frame or beginning the next calculation iteration.

for example if I want the program to wait .05 seconds between frames and the AI takes .02 second to reach a conclusion it would be preferable to sleep only .03 seconds making the total time between frames .05 rather than sleeping a constant .05 seconds and ending up with .07 seconds between frames.