Differences and linkages between thread.sleep () and TimeUnit.SECONDS.sleep ()

Source: Internet
Author: User

It was strange to see the TimeUnit.SECONDS.sleep () method, how does the sleep method also be provided here?  Public voidSleepLongTimeoutthrowsinterruptedexception {if(Timeout > 0) {        Longms =Tomillis (timeout); intNS =Excessnanos (timeout, MS);    Thread.Sleep (MS, NS); }} results a look at the source code, The original is the packaging of the Thread.Sleep method, the implementation is the same, just a lot of time unit conversion and validation, but the method of Timeunit enumeration members to provide better readability, this may be the first time to create a sleep method Timeunit, we all know that the sleep method is very common, but often To use a constant to save the time of sleep, such as 3 seconds, our code usually writes:Private Final intSleep_time = 3 * 1000;//3 secondsbecause the Thread.Sleep method parameter accepts the numeric value of the millisecond unit, the following code knows that the Timeunit enumeration member's Sleep method is more elegant: TimeUnit.MILLISECONDS.sleep (10); TimeUnit.SECONDS.sleep (10); TimeUnit.MINUTES.sleep (10); Thread.Sleep (10); Thread.Sleep (10*1000); Thread.Sleep (10*60*1000but does the sleep method using the Timeunit enumeration member bring a performance penalty, after all, increases the function call overhead? Test the test bar:ImportJava.util.concurrent.TimeUnit; Public classTestsleep { Public Static voidMain (string[] args)throwsinterruptedexception {sleepbytimeunit (10000); Sleepbythread (10000); }    Private Static voidSleepbytimeunit (intSleeptimes)throwsinterruptedexception {LongStart =System.currenttimemillis ();  for(inti=0; i<sleeptimes; i++) {TimeUnit.MILLISECONDS.sleep (10);                                                                                                                           } LongEnd =System.currenttimemillis (); System.out.println ("Total time consumed by TimeUnit.MILLISECONDS.sleep:" + (End-start)); }                                                                                                                   Private Static voidSleepbythread (intSleeptimes)throwsinterruptedexception {LongStart =System.currenttimemillis ();  for(inti=0; i<sleeptimes; i++) {Thread.Sleep (10);                                                                                                                           } LongEnd =System.currenttimemillis (); System.out.println ("Total time consumed by Thread.Sleep:" + (End-start)); }} Two test results (Win7+4g+JDK7 during testing): Total time consumed by TimeUnit.MILLISECONDS.sleep:100068total time consumed by Thread.Sleep:100134difference:---66total time consumed by TimeUnit.MILLISECONDS.sleep:100222total time consumed by Thread.Sleep:100077difference:--+145from the results you can see that 10,000 calls differ very little, even once faster, without excluding the JVM being optimized, and if performance considerations are ignored, it is recommended to use the Timeunit enumeration member's sleep method for readability. In addition Timeunit is a good example of enumeration implementation, Doug Lea too God, admire pleasantly surprised! Source: http://stevex.blog.51cto.com/4300375/1285767

Differences and linkages between thread.sleep () and TimeUnit.SECONDS.sleep ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.