Sleep () and thread. In the timeunit class sleep()

What is timeunit?

Timeunit is Java util. A class under the concurrent package, timeunit, provides a better readable thread pause operation, which is usually used to replace thread Sleep (). For a long time, the sleep () method of thread has been the standard way to pause threads. Almost all Java programmers are familiar with it. In fact, the sleep method itself is also very common and appears in many interviews. If you have used thread Sleep (), of course, I'm sure you've done this, so you must know that it's a static method. It won't release the lock when pausing the thread. This method will throw an interrupttedexception (if a thread interrupts the current thread). But a potential problem that many of us don't pay attention to is its readability. Thread. Sleep () is an overloaded method, It can receive long integer milliseconds and long integer nanoseconds parameters. One problem for programmers is that it is difficult to know how many seconds, minutes, hours or days the current thread has slept. Look at this thread Sleep() method:

At first glance, can you calculate how long the current thread is waiting? Maybe some people can, but for most programmers, the readability of this writing method is still very poor. You need to convert milliseconds into seconds and minutes. Let's take a look at another example. This example is slightly better than the previous example:

This is much better than the previous example, but it is still not the best. You notice that the sleep time is in milliseconds. 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 see how threads sleep for 4 minutes and use timeunit.

Similarly, you can pause the current thread at the second, minute and hour levels. As you can see, this is much more readable than thread's sleep method. Remember timeunit Thread. Called internally by sleep() Sleep () will also throw interruptexception. You can also check the JDK source code to verify it. The following is a simple example that shows how to use timeunit Sleep() method.

In addition to the sleep function, timeunit also provides a convenient method to convert time into different units. For example, if you want to convert seconds into milliseconds, you can use the following code:

It will return 44000

TimeUnit vs Thread. sleep()

At present, we discuss that the advantage of using timeunit is to improve readability, but sometimes we think other methods are better because thread Sleep () appeared with Java very early, and almost all programmers know thread Sleep(), which is known to suspend the current thread, is not familiar with timeunit. Two reasons: one is to compare thread Sleep(), timeunit is not very commonly used. The second reason is that it is not in the thread class, just like wait and notify are not in the thread. Anyway, these will take some time to be adopted and become a standard method.

To sum up, before you want to use thread Where the sleep () method is used, you'd better use timeunit Sleep () method instead. It can not only improve the readability of the code, but also be more familiar with Java util. Concurrent package, because timeunit is also a key API in concurrent programming.

At the same time, you are also welcome to add QQ group (884650212) for communication!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>