Share a sleep method more elegant wording

The original use of sleep method is this:

Thread.sleep(1000);

Follow up on sleep () method source code look:

 public static native void sleep(long millis) throws InterruptedException;

It can be found in a local method, but here is more critical parameter is the long millis type, that is to say whether we are to sleep one second, one minute, one hour, one day, and we have to be converted into milliseconds as a parameter the correct result is what we want.

Use another elegant wording

TimeUnit.SECONDS.sleep(1);

This code is equivalent to [Thread.sleep (1000)]

Here Insert Picture Description

You can see from the screenshot, there are many units have been defined, now get active, without calculation.

to sum up

Simply made a more elegant method of sleep wording, because I think that using the second method, we can not only go a little less computing time, other developers can easily see the specific time to sleep, I can not let we look at other code that people who go to count again (not everyone will write comments, so the code can be used to solve the reading problem, do not write in the comments).

Published 227 original articles · won praise 229 · views 40000 +

Guess you like

Origin blog.csdn.net/weixin_38106322/article/details/104589575