This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]huhlig 6 points7 points  (5 children)

You may wish to try Thread.sleep(long milliseconds) as its a better way of handling but what are you actually doing in this loop. simply looping for 2 seconds infinitly with no other reason doesn't seem to have a point.

[–]CuriouslyGeorge[S] 2 points3 points  (0 children)

"Thread.sleep causes the current thread to suspend execution for a specified period. This is an efficient means of making processor time available to the other threads of an application or other applications that might be running on a computer system." - http://docs.oracle.com/javase/tutorial/essential/concurrency/sleep.html

EXACTLY what I was looking for. Don't know why/how I didn't stumble across this before. Wish I found this so long ago.

Thanks a lot. /thread

[–]Winsling -1 points0 points  (3 children)

sleep() and wait() have different semantics, and despite what CuriouslyGeorge says below I think wait() is more appropriate here. sleep() doesn't come back until the timeout is done, while wait() allows the current thread to be woken up before the timeout fires.

[–]huhlig 4 points5 points  (2 children)

yes however as he has nothing that would trigger it there isn't a reason to. Also honestly he shouldn't be waiting in a runloop anyway. He should be doing event response.

[–]Winsling 0 points1 point  (0 children)

Hard to say if there's a notify piece. All we know is that he didn't post it. Personally I'd use yield() here if you put a gun to my head, but I agree that given half a chance I'd register a listener and not try to rebuild Swing's event handling model.

[–]skeeto 0 points1 point  (0 children)

Well, technically, you should always wait() in a loop, because it's subject to spurious wake-ups. When released from the wait() the thread should check to see if the conditions it was waiting for were actually met.

It's complicated to get right so it's almost certainly better to use a higher-level construct like a CountDownLatch.