I am trying to make an intro to a game with some strings that I want one to wait for another to pop up, and I don't directly want to use Thread.sleep() for it to wait, because I am not sure if that is the best option. Is there any other way to make something wait than making the thread sleep, or will I just have to make the thread sleep?
-
7This gets asked a lot. Answer: use a Swing Timer. – Hovercraft Full Of Eels Jun 30 '13 at 20:33
-
see this http://stackoverflow.com/questions/1006611/java-swing-timer – tostao Jun 30 '13 at 20:34
-
When I searched it up on google I didn't find anything on a timer. Thanks. – Shzylo Jun 30 '13 at 20:34
-
of course, you need notify the loading screen by the loading core module, the loading screen try to say loading and just waits for the any notification, after content got loaded, jut remove the screen [some help here](http://arashmd.blogspot.com/2013/06/java-threading.html#synctr1) – Jun 30 '13 at 20:41
-
This belongs in Programmers not SO so at least move it instead of trying to close it. – Andrew T Finnell Jun 30 '13 at 20:45
-
@Shzylo Serioisly? Take a look at the first hit for this [Google search](https://www.google.com.au/search?q=java+timer&ie=UTF-8&oe=UTF-8&hl=en-gb&client=safari#sclient=tablet-gws&safe=off&client=safari&hl=en-gb&q=java+swing+timer&oq=java+swing+timer&gs_l=tablet-gws.3..0l2j0i7.10173.11478.0.12443.6.6.0.0.0.4.687.2269.2-3j1j1j1.6.0...0.0...1c.1.18.tablet-gws-psy.rPQhKrwKGlQ&pbx=1&bav=on.2,or.&bvm=bv.48572450,d.dGI&fp=e1ee6af394e608fe&biw=768&bih=928) – MadProgrammer Jun 30 '13 at 21:14
-
I didn't google that. I didn't even think of timers at the time, please don't mock me. – Shzylo Jun 30 '13 at 22:01
4 Answers
If this is a game you shouldn't use sleeps or timers.
Typically games have their own internal clock mechanism. This means you will try to render the frames as fast as possible. Your OnRender method will be invoked with the current time of the game. You can use this to determine if enough time has passed to go to the next screen.
This means you will be given a point in time A in frame 1. You'll be given the Delta or another point in time B in frame 2. You can determine how much time has passed by using the delta or calculating the delta yourself. This is a very efficient mechanism for timing situations and worked quite well when games were single threaded. The idea of any program is to never block for anything.
The reasons things typically block is due to I/O such as reading from disk, the network or putting data on the GPU. In your situation you can do everything without blocking.
Here is a decent page on this https://gamedev.stackexchange.com/questions/1589/fixed-time-step-vs-variable-time-step

- 1
- 1

- 13,417
- 3
- 33
- 49
There's a standard mechanism for this: Object.wait() and Object.notify() (with their overloads / variants). You simply wait for some event to occur in one thread, and some other thread is responsible for notifying you (or everyone, in case of notifyAll
) of that occurrence.
You can also make use of the new Condition mechanism introduced in java.util.concurrent
.

- 37,042
- 7
- 56
- 92
-
Does the thread sleep indefinitely? Any probability to be unable to wakeup like a deadlock? – huseyin tugrul buyukisik Jun 30 '13 at 20:48
-
1Only if you're waiting for a notification that never comes. Any decent code must guarantee that notifications do eventually occur. However, you can always `wait` with a `timeout` to make sure the execution resumes at some point. – Costi Ciudatu Jun 30 '13 at 20:50
-
So, this gives the ultimate cpu resources when there are hundreds of threads(ofcourse only needed ones are awaken up with this)? – huseyin tugrul buyukisik Jun 30 '13 at 20:51
-
I'm not sure I got the question, but the wait/notify mechanism in Java is *not* busy waiting, if that's your concern. – Costi Ciudatu Jun 30 '13 at 20:58
-
I mean, threads not automatically switched. Just when a thread orders notify, then it switches. So only necessary switching is done right? – huseyin tugrul buyukisik Jun 30 '13 at 21:01
-
Can a thread wait itself when finishes its first iteration? (an example of a daemon thread) So another controller thread can wake it up? – huseyin tugrul buyukisik Jun 30 '13 at 21:06
-
let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/32650/discussion-between-costi-ciudatu-and-huseyin-tugrul-buyukisik) – Costi Ciudatu Jun 30 '13 at 21:09
-
long t1=0,t2=0;
long nanoWaitTime=10000; //to wait at least 10000 nano-seconds
t1=System.nanoTime();
//start waiting
long count=0;
boolean releaseCpuResources=true;
while(Math.abs(t2-t1)<nanoWaitTime)
{
t2=System.nanoTime(); //needs maybe 1000 cycles of cpu to get this value.
//so this is like busy-wait
//and minimum step may be 1 micro-seconds or more
if(releaseCpuResources)
{
count++;
if(count>1000)Thread.sleep(1);//after too many iterations, cpu gets overwhelmed
//so Thread.sleep makes it better for large waiting
//times
//but precision is lost. Like uncertainity principle
//but in a quantized fashion
}
}
// here continue to work after waiting
The resolution or precision may not be what you want in for all cpus.

- 11,469
- 4
- 45
- 97
-
1busy waits are inefficient, however more accurate than thread.sleep. – William Morrison Jun 30 '13 at 20:38
-
Can we put something like "nop" from assembly? Does it count as busy wait too? – huseyin tugrul buyukisik Jun 30 '13 at 20:39
-
Don't think java has an equivalent. Long as the wait time isn't too high yours is a fine approach. Now if you're waiting 5 seconds, just watch your CPU climb. You'll use a whole core. – William Morrison Jun 30 '13 at 20:41
-
-
You are right. Can we delay this waiting after 20 while-iterations? So this go off and another thread catches a breath. Lets say that this is gone too much and "lets sleep with Thread.sleep(1)" because its better for big miliseconds time waitin. – huseyin tugrul buyukisik Jun 30 '13 at 20:42
-
Nah I wouldn't try a hybrid approach like that. Just use busy wait/spin wait for small times when precision is very important, use Thread.sleep for longer waits when precision is less important. If you need high precision, long sleep time I'd just search out another solution. Maybe use some special OS level timer or something. – William Morrison Jun 30 '13 at 20:51
-
Costi Ciatiu's answer seems to be the best if precision is not important. – huseyin tugrul buyukisik Jun 30 '13 at 20:53
-
Sidenote on busy waits - the HotSpot JVM does this when a thread attempts to take a contested lock - it spins for a few cycles to see if it really needs to park itself. – selig Jun 30 '13 at 21:00
-
If you're making this in a game, why not try using something like Actions in libgdx? You just chain different actors together. Whenever a property like visibility or position reaches the value you want, you trigger the next action. Properties conditions are checked during each update loop of your game.
Or if its a swing app, use a timer to check these properties.

- 10,953
- 2
- 31
- 48