Why Thread.sleep is bad to use Why Thread.sleep is bad to use multithreading multithreading

Why Thread.sleep is bad to use


It's fine to use Thread.sleep in that situation. The reason people discourage Thread.sleep is because it's frequently used in an ill attempt to fix a race condition, used where notification based synchronization is a much better choice etc.

In this case, AFAIK you don't have an option but poll because the API doesn't provide you with notifications. I can also see it's a infrequent operation because presumably you are not going to create thousand tables.

Therefore, I find it fine to use Thread.sleep here. As you said, spawning a separate thread when you are going to block the current thread anyways seems to complicate things without merit.


Yes, one should try to avoid usage of Thread.sleep(x) but it shouldn't be totally forgotten:

Why it should be avoided

  • It doesn't release the lock
  • It doesn't gurantee that the execution will start after sleeping time (So it may keep waiting forever - obviously a rare case)
  • If we mistakenly put a foreground processing thread on sleep then we wouldn't be able to close that application till x milliseconds.
  • We now full loaded with new concurrency package for specific problems (like design patterns (ofcourse not exactly), why to use Thread.sleep(x) then.

Where to use Thread.sleep(x):

  • For providing delays in background running threads
  • And few others.