aspose file tools*
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
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: 378
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: 378


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: 4240
    
    7

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: 396

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: is thread.sleep safe?
 
Similar Threads
Setting Up A Delay Between E-mail tranmissions
Garbage Collector
Looking for a logic to insert delay in our java code
WA #1.....word association
How to wait for a certain amount of idle time ?