What is timeunit?
Timeunit is Java. util. A class in the concurrent package. timeunit provides a thread pause operation with better readability, which is usually used to replace the thread. sleep (), for a long time, the thread's sleep () method is used as the standard way to suspend a thread. Almost all Java programmers are familiar with it, in fact, the sleep method is also very common and appears in many interviews. If you have used thread. sleep (), of course, I'm sure you have done this, so you must be familiar with it as a static method. When the thread is paused, it will not release the lock, this method throws an interrupttedexception exception (if a thread interrupts the current thread ). However, a potential problem that many of us do not pay attention to is its readability. Thread. sleep () is an overload method that can receive parameters of long integer milliseconds and long integer nanoseconds, this makes it difficult for programmers to know how many seconds, minutes, hours, or days the current thread is sleeping. Take a look at the following thread. Sleep () method:
Thread.sleep(2400000)
Roughly speaking, Can you calculate how long the current thread is waiting? Some people may, but for most programmers, this writing method is quite readable. You need to convert milliseconds to seconds and minutes. Let's take a look at another example, this example is slightly more readable than the previous example:
Thread.sleep(4*60*1000);
This is much better than the previous example, but it is still not the best. You notice that the sleep time is in milliseconds and it is not easy to guess that the current thread will wait for 4 minutes. The timeunit class solves this problem by specifying days, hours, minutes, seconds, milliseconds, and nanoseconds. Java. utils. Concurrent. timeunit is one of the best examples in Java enumeration application scenarios. All timeunits are enumeration instances. Let's take a look at how timeunit is used when a thread sleeps for 4 minutes.
TimeUnit.MINUTES.sleep(4); // sleeping for 1 minutes
Similarly, you can pause the current thread in seconds, minutes, and hours. You can see that this is much more readable than the thread sleep method. Remember that the thread. Sleep () internally called by timeunit. Sleep () will also throw interruptexception. You can also check the JDK source code for verification. The following is a simple example that shows how to use the timeunit. Sleep () method.
/** * * Java program to demonstrate how to use TimeUnit.sleep() method in Java. * TimeUnit is a new way of introducing pause in Java program. * @author Javin */public class TimeUnitTest { public static void main(String args[]) throws InterruptedException { System.out.println("Sleeping for 4 minutes using Thread.sleep()"); Thread.sleep(4 * 60 * 1000); System.out.println("Sleeping for 4 minutes using TimeUnit sleep()"); TimeUnit.SECONDS.sleep(4); TimeUnit.MINUTES.sleep(4); TimeUnit.HOURS.sleep(1); TimeUnit.DAYS.sleep(1); }}
In addition to sleep, timeunit also provides a convenient way to convert time to different units. For example, if you want to convert seconds to milliseconds, you can use the following code:
TimeUnit.SECONDS.toMillis(44)
It will return 44,000
TimeUnit vs Thread.sleep()
At present, the advantage of using timeunit is to improve readability, but sometimes we think other methods are better, because thread. sleep () came along with Java for a long time, and almost all programmers know thread. sleep (), we all know that the current thread is suspended, but not very familiar with timeunit. Two reasons: First, compare thread. sleep (), timeunit is not very commonly used, and the second is that it is not in the Thread class, just as wait and policy are not in the Thread class. It takes some time to adopt these functions, and become a standard method.
To sum up, you 'd better use the timeunit. Sleep () method instead of the thread. Sleep () method. It can not only improve code readability, but also become more familiar with the java. util. Concurrent package, because timeunit is also a key API in concurrent programming.
Use sleep () in the timeunit class first ()