File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Threads and Synchronization and the fly likes is thread.sleep safe? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply locked New topic

is thread.sleep safe?

Azrael Noor
Ranch Hand

Joined: Jul 29, 2010
Posts: 384
is thread.sleep safe if used in while(true) loop?

Azrael Noor
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

How do you mean safe? Your computer won't blow up if you use it like that. So yea in that way it is safe.

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Azrael Noor
Ranch Hand

Joined: Jul 29, 2010
Posts: 384

but my computer once gone in loop is not allowing to stop the execution of application. Because i used Thread.Sleep in Loop.
Matthew Brown

Joined: Apr 06, 2010
Posts: 4544

Doesn't that sound like the while(true) is more likely to be a problem than Thread.sleep()? If you're going to go into a loop, make sure there's a way out of it.
Chris Hurst
Ranch Hand

Joined: Oct 26, 2003
Posts: 443

What you doing about InterruptedException handling ...

presumably your logic should be catch InterruptedException determine application stop is required and break out of loop.
How are you triggering, signalling stop ??

You could make the thread your sleeping in a daemon thread , depends on what its purpose is.

We need some code really ;-)

"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

I should probably mention (though Azrael Noor you should really have said this too) this is a cross post and the code in question is a Servlet. Please continue the discussion here.

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
I agree. Here's the link:
subject: is thread.sleep safe?
It's not a secret anymore!