tag:blogger.com,1999:blog-8712770457197348465.post2006264027034761975..comments2024-03-28T02:47:19.159-07:00Comments on Javarevisited: Prefer TimeUnit Sleep over Thread.Sleep - Java Coding Tips Examplejavin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger9125tag:blogger.com,1999:blog-8712770457197348465.post-18025902578916222202020-12-26T20:26:13.024-08:002020-12-26T20:26:13.024-08:00Hello @Anonymous, those 4 calls are there to illus...Hello @Anonymous, those 4 calls are there to illustrate that how you can make it more explicit calls using TimeUnit, they are not equivalent to the Thread.sleep() method. They are just there that how you can use them. javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-80653646973282533472020-12-22T04:10:03.303-08:002020-12-22T04:10:03.303-08:00One big difference is, that just in this example, ...One big difference is, that just in this example, you invoke sleep four times. All of those sleep calls need to return so that the next one can be called. That does not make any sense and is, in my opinion, really bad code style and unnecessary overhead. Everyone understands Thread.sleep(4*60*1000) by just looking at it, if he understands time. And that should be around being in second grade or Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-7880269696721707872018-01-25T01:30:47.397-08:002018-01-25T01:30:47.397-08:00TimeUnit.sleep( internally calls thread.sleep. no ...TimeUnit.sleep( internally calls thread.sleep. no much difference<br />public void sleep(long timeout) throws InterruptedException {<br /> if (timeout > 0) {<br /> long ms = toMillis(timeout);<br /> int ns = excessNanos(timeout, ms);<br /> Thread.sleep(ms, ns);<br /> }<br /> }Anonymoushttps://www.blogger.com/profile/13152399443720646688noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-11118816961617722962015-02-13T07:27:23.125-08:002015-02-13T07:27:23.125-08:00One reason to prefer TimeUnit's sleep() method...One reason to prefer TimeUnit's sleep() method is readability other could be that Thread.sleep(0) is not clearly defined across platforms. In TimeUnit it does nothingAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-13358174014261752252015-02-13T07:26:33.625-08:002015-02-13T07:26:33.625-08:00TimeUnit.sleep(0) does not behave like Thread.slee...TimeUnit.sleep(0) does not behave like Thread.sleep(0). Is that intentional?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-71350539428037508922014-07-08T06:33:54.923-07:002014-07-08T06:33:54.923-07:00It is all about readability, that is the point of ...It is all about readability, that is the point of the article.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-44147770720402549712014-06-19T10:28:26.102-07:002014-06-19T10:28:26.102-07:00TimeUnit calls Thread.sleep() so it's not much...TimeUnit calls Thread.sleep() so it's not much of a difference...Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-30966883841991840312012-12-14T20:31:08.487-08:002012-12-14T20:31:08.487-08:00Java 5 only introduced some of the enumeration. J...Java 5 only introduced some of the enumeration. Java 6 introduced MINUTES, HOURS, DAYS along with related methods.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-80854636087363257662012-12-13T18:29:02.252-08:002012-12-13T18:29:02.252-08:00TimeUnit.MINUTES.sleep(4); // sleeping for 1 minu...TimeUnit.MINUTES.sleep(4); // sleeping for 1 minutes<br /><br />should be:<br /><br />TimeUnit.MINUTES.sleep(4); // sleeping for 4 minutesAnonymousnoreply@blogger.com