Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In which circumstances is Thread.sleep() the best approach for pausing a thread?

Thread.sleep() to me seems like a really useless concept because the only way I see it working is in the following scenarios

 public void someFunction()
 {
      //thread does something
      Thread.sleep(50000);
      //now other threads do something
 }

The problem is that for me this is just asking for trouble. I mean this can either be to long in which case at best you may have a performance issue and it may be too long and other threads may wait for the result in which case the situation may become critical.

I mean there are other way like using synchronized and wait which seem much more efficient.

So my question is, are there any situations where using thread sleep is the best option?

like image 559
ciprianr Avatar asked Dec 01 '25 00:12

ciprianr


1 Answers

It would be impossible to write java.util.Timer without a sleep() method, or at least it would require you to abuse the wait() method, and write a lot of extra code around it to protect against spurious wakeups.

like image 149
user207421 Avatar answered Dec 03 '25 13:12

user207421



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!