This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply locked New topic
Author

is thread.sleep safe?

Azrael Noor
Ranch Hand

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


Regards
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: 382


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
Bartender

Joined: Apr 06, 2010
Posts: 4344
    
    8

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: 412
    
    2

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
Bartender

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
 
jQuery in Action, 2nd edition
 
subject: is thread.sleep safe?
 
Similar Threads
WA #1.....word association
How to wait for a certain amount of idle time ?
Garbage Collector
Looking for a logic to insert delay in our java code
Setting Up A Delay Between E-mail tranmissions