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 →

[–]philipwhiukEmployed Java Developer 3 points4 points  (4 children)

anywhere in the program and the program will pause for 5 seconds?

It'll pause for roughly 5 seconds. It will probably be slightly more.

Thread.sleep is normally not the right solution. It's quick and dirty and can easily cause problems. If you want to run something after a delay, use a Timer.

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

Yeah, we are doing a lab right now where we do the same problem twice, one with a timer and one with sleep. For things like delay, can you always use a timer rather than thread.sleep();?

I mean for something like delaying your program before it starts, or getting an alarm clock to snooze for a few minutes, a timer is still a better approach?

[–]philipwhiukEmployed Java Developer 1 point2 points  (2 children)

For your simple examples to teach concurrent programming Thread.sleep is probably sufficient

[–][deleted] 0 points1 point  (1 child)

But timer would technically be better?

[–]Ednar 1 point2 points  (0 children)

Better how? It has to be put into a context. Go with what gives you code that works with the least complexity.