java.util.concurrent.TimeUnit 桃扇骨 2023-07-22 11:27 1阅读 0赞 > api:[https://docs.oracle.com/javase/8/docs/api/index.html][https_docs.oracle.com_javase_8_docs_api_index.html] -------------------- # 1 TimeUnit # > [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/TimeUnit.html][https_docs.oracle.com_javase_8_docs_api_java_util_concurrent_TimeUnit.html] > TimeUnit是java.util.concurrent包下面的一个类;TimeUnit 是Java**枚举**应用场景中最好的例子之一,所有TimeUnit都是枚举实例; > > TimeUnit提供了更加优雅的线程sleep写法;TimeUnit还提供了各种时间单位转换的方法; NANOSECONDS //毫微秒 十亿分之一秒【1微秒/1000】 MICROSECONDS //微秒 一百万分之一秒【1毫秒/1000】 MILLISECONDS //毫秒 千分之一秒 SECONDS //秒 MINUTES //分钟 HOURS //小时 DAYS //天 //NANOSECONDS MICROSECONDS MILLISECONDS SECONDS MINUTES HOURS DAYS Arrays.stream(TimeUnit.values()) .forEach((item -> System.out.print(item + " "))); -------------------- # 2 线程睡眠 # <table> java.lang.Thread <tbody> <tr> <th style="width:165px;">Modifier and Type</th> <th>Method and Description</th> </tr> <tr> <td style="width:165px;"><code>static void</code></td> <td><code><a href="https://docs.oracle.com/javase/8/docs/api/java/lang/Thread.html#sleep-long-" rel="nofollow">sleep</a>(long millis)</code> <p>Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds, subject to the precision and accuracy of system timers and schedulers.</p> </td> </tr> <tr> <td style="width:165px;"><code>static void</code></td> <td><code><a href="https://docs.oracle.com/javase/8/docs/api/java/lang/Thread.html#sleep-long-int-" rel="nofollow">sleep</a>(long millis, int nanos)</code> <p>Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds plus the specified number of nanoseconds, subject to the precision and accuracy of system timers and schedulers.</p> </td> </tr> </tbody> </table> > TimeUnit提供了更加优雅的线程睡眠写法 //休眠40分种的三种写法 // Thread.sleep(2400000); // Thread.sleep(40 * 60 * 1000); // 可读性增强了 TimeUnit.MINUTES.sleep(40); -------------------- # 3 时间单位转换 # //sleep 40分组 Thread.sleep(2400000); Thread.sleep(TimeUnit.MINUTES.toMillis(40)); <table> <tbody> <tr> <th>Modifier and Type</th> <th>Method and Description</th> </tr> <tr> <td><code>long</code></td> <td><code><a href="https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/TimeUnit.html#convert-long-java.util.concurrent.TimeUnit-" rel="nofollow">convert</a>(long sourceDuration, <a href="https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/TimeUnit.html" rel="nofollow">TimeUnit</a> sourceUnit)</code> <p>Converts the given time duration in the given unit to this unit.</p> </td> </tr> </tbody> </table> //天转化成小时 TimeUnit.HOURS.convert(3, TimeUnit.DAYS); //72 //小时转化为秒 TimeUnit.SECONDS.convert(1, TimeUnit.HOURS);//3600 <table> <tbody> <tr> <th>Modifier and Type</th> <th>Method and Description</th> </tr> <tr> <td><code>long</code></td> <td><code><a href="https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/TimeUnit.html#toDays-long-" rel="nofollow">toDays</a>(long duration)</code> <p>Equivalent to <a href="https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/TimeUnit.html#convert-long-java.util.concurrent.TimeUnit-" rel="nofollow"><code>DAYS.convert(duration, this)</code></a>.</p> </td> </tr> </tbody> </table> //天转化为小时 TimeUnit.DAYS.toHours(3); //72 //小时转化为秒 TimeUnit.HOURS.toSeconds(1); //3600 [https_docs.oracle.com_javase_8_docs_api_index.html]: https://docs.oracle.com/javase/8/docs/api/index.html [https_docs.oracle.com_javase_8_docs_api_java_util_concurrent_TimeUnit.html]: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/TimeUnit.html
还没有评论,来说两句吧...