日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java.util.concurrent.TimeUnit

發布時間:2025/3/12 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java.util.concurrent.TimeUnit 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

JavaAPI:https://docs.oracle.com/javase/8/docs/api/index.html


1 TimeUnit

api: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 線程睡眠

Modifier and TypeMethod and Description
static voidsleep(long millis)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.
static voidsleep(long millis, int nanos)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.

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)); Modifier and TypeMethod and Description
longconvert(long sourceDuration, TimeUnit sourceUnit)Converts the given time duration in the given unit to this unit.
//天轉化成小時 TimeUnit.HOURS.convert(3, TimeUnit.DAYS); //72 //小時轉化為秒 TimeUnit.SECONDS.convert(1, TimeUnit.HOURS);//3600 Modifier and TypeMethod and Description
longtoDays(long duration)Equivalent to DAYS.convert(duration, this).
//天轉化為小時 TimeUnit.DAYS.toHours(3); //72 //小時轉化為秒 TimeUnit.HOURS.toSeconds(1); //3600 創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的java.util.concurrent.TimeUnit的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。