What is TimeUnit in Java
TimeUnit in Java is a class on java.util.concurrent package, introduced in Java 5 along with CountDownLatch, CyclicBarrier, Semaphoreand 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. Difference between wait and sleep is one of the tough Java question to answer. If you have used Thread.sleep() before and I am sure you do, you might be familiar with fact like it's an static method, it doesn't release lock when pausing Thread and it throws InterruptedException. But what many of us doesn't consider a potential issue is readability. Thread.sleep() is an overloaded method and accept long millisecond and longnanosecond, which makes it hard for programmers to find out exactly how many seconds, minutes, hours or day current Thread is sleeping. Look at below example of Thread's sleep() method:
Read more »
TimeUnit in Java is a class on java.util.concurrent package, introduced in Java 5 along with CountDownLatch, CyclicBarrier, Semaphoreand 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. Difference between wait and sleep is one of the tough Java question to answer. If you have used Thread.sleep() before and I am sure you do, you might be familiar with fact like it's an static method, it doesn't release lock when pausing Thread and it throws InterruptedException. But what many of us doesn't consider a potential issue is readability. Thread.sleep() is an overloaded method and accept long millisecond and longnanosecond, which makes it hard for programmers to find out exactly how many seconds, minutes, hours or day current Thread is sleeping. Look at below example of Thread's sleep() method:
Thread.sleep(2400000);