Java : Use TimeUnit.sleep instead of Thread.sleep
The Thread.sleep method with milliseconds is often used to cease a currently executing thread. The TimeUnit class also has the sleep method. Using the TimeUnit class makes the code a little more readable than Thread.sleep.
Note:
- The Thread.sleep(Duration duration) method was added from Java 19. If you use Java 19 or later, it's also recommended to use the Thread.sleep(Duration duration) method.
- This article may use translation software for your convenience. Please also check the original Japanese version.
Thread.sleep(long millis)
Thread.sleep ceases a thread for the specified length of milliseconds.
For example, to sleep for 5 seconds (5 seconds = 5000 milliseconds) :
final var start = System.nanoTime();
Thread.sleep(5000);
final var end = System.nanoTime();
// 5.005457 sec.
System.out.printf("%f sec.%n", (end - start) / 1000000000.0);
You can sleep the thread for about 5 seconds.
Note:
- System.nanoTime is used to measure processing time. It returns a value in nanoseconds, so you convert it to seconds by dividing by 1000000000.
If you're a regular Java user, you might look at Thread.sleep(5000) and immediately know that it should stop for 5 seconds. If not, you may have forgotten the unit of sleep. In that case, you will have to check the API specification. It loses a bit of time.
TimeUnit.sleep
A TimeUnit is an enum class representing the unit of time. The TimeUnit has the following enum Constants.
- DAYS
- HOURS
- MINUTES
- SECONDS
- MILLISECONDS
- MICROSECONDS
- NANOSECONDS
Although the TimeUnit is an enum, it provides some utility methods. sleep is one of them.
Now let's look at an example that uses the TimeUnit to sleep for 5 seconds.
final var start = System.nanoTime();
TimeUnit.SECONDS.sleep(5);
final var end = System.nanoTime();
// 5.004578 sec.
System.out.printf("%f sec.%n", (end - start) / 1000000000.0);
You can sleep the thread for about 5 seconds.
Thread.sleep(5000);
TimeUnit.SECONDS.sleep(5);
Compared to Thread.sleep, the amount of typing has increased somewhat. However, it becomes clear to sleep for 5 seconds.
Conclusion
Sleep time | TimeUnit | Thread |
---|---|---|
300 milliseconds |
|
|
5 seconds |
|
|
1 minute |
|
|
How do you feel when you compare them side by side? Do you find TimeUnit to be easier to understand?
It may be just a little difference.
If you're the only one reading the code, you probably don't need to worry too much about code readability. However, when developing in a team, you would like to improve the readability even a little.
Related posts
- API Examples
- BlockingQueue
- Callable
- CancellationException
- ConcurrentHashMap.KeySetView
- ConcurrentLinkedDeque
- ConcurrentLinkedQueue
- ConcurrentMap
- ConcurrentModificationException
- ConcurrentSkipListSet
- Condition
- CopyOnWriteArrayList
- CopyOnWriteArraySet
- CountDownLatch
- CyclicBarrier
- Exchanger
- Executor
- ExecutorService
- Executors
- Future
- Future.State
- FutureTask
- InterruptedException
- Lock
- Runnable
- Semaphore
- Thread
- ThreadGroup
- ThreadLocal
- TimeUnit