Posts

Showing posts with the label concurrency example and tutorial

Prefer TimeUnit Sleep over Thread.Sleep - Java Coding Tips

What is TimeUnit in Java TimeUnit in Java is a class on java.util.concurrent package, introduced in Java 5 along with CountDownLatch , CyclicBarrier , Semaphore and several other concurrent utilities. TimeUnit provides a human readable version of Thread.sleep() method which can be used in place of former. From long time Thread's sleep() method is standard way to pause a Thread in Java and almost every Java programmer is familiar with that. In fact, sleep method itself is a very popular and has appeared on many Java interviews. The difference between wait and sleep is one of the tough Java questions to answer. If you have used Thread.sleep() before and I am sure you do, you might be familiar with a fact like it's a static method, it doesn't release the lock when pausing Thread and it throws InterruptedException . Read more �