java.util.concurrent.TimeUnit
阿新 • • 發佈:2019-01-02
- TimeUnit提供了可讀性更好的執行緒暫停操作,通常用來替換Thread.sleep(),
在很長一段時間裡Thread的sleep()方法作為暫停執行緒的標準方式,幾乎所有Java程式設計師都熟悉它,事實上sleep方法本身也很常用而且出現在很多面試中。
- 如果你已經使用過Thread.sleep(),當然我確信你這樣做過,那麼你一定熟知它是一個靜態方法,暫停執行緒時它不會釋放鎖,該方法會丟擲InterrupttedException異常(如果有執行緒中斷了當前執行緒),TimeUnit.sleep()內部呼叫的Thread.sleep()也會丟擲InterruptException。
- 但是我們很多人並沒有注意的一個潛在的問題就是它的可讀性。Thread.sleep()是一個過載方法,可以接收長整型毫秒和長整型的納秒引數,這樣對程式設計師造成的一個問題就是很難知道到底當前執行緒是睡眠了多少秒、分、小時或者天。
- TimeUnit類解決了這個問題,通過指定DAYS、HOURS、MINUTES,SECONDS、MILLISECONDS和NANOSECONDS。java.utils.concurrent .TimeUnit 是Java列舉應用場景中最好的例子之一,所有TimeUnit都是列舉例項,讓我們來看看執行緒睡眠4分鐘用TimeUnit是如何使用的。
TimeUnit.MINUTES.sleep(4); // sleeping for 4 minutes
除了sleep的功能外,TimeUnit還提供了便捷方法用於把時間轉換成不同單位,例如,如果你想把秒轉換成毫秒,你可以使用下面程式碼:TimeUnit.SECONDS.toMillis(44)
-
TimeUnit常用方法
//關於秒的常用方法 TimeUnit.SECONDS.toMillis(1) 1秒轉換為毫秒數 TimeUnit.SECONDS.toMinutes(60) 60秒轉換為分鐘數 TimeUnit.SECONDS.sleep(5) 執行緒休眠5秒 TimeUnit.SECONDS.convert(1, TimeUnit.MINUTES) 1分鐘轉換為秒數 //TimeUnit.DAYS 日的工具類 //TimeUnit.HOURS 時的工具類 //TimeUnit.MINUTES 分的工具類 //TimeUnit.SECONDS 秒的工具類 //TimeUnit.MILLISECONDS 毫秒的工具類
方法詳細資訊
values
public static final TimeUnit[] values()
- Returns an array containing the constants of this enum type, in the order they are declared. This method may be used to iterate over the constants as follows:
for(TimeUnit c : TimeUnit.values()) System.out.println(c);
-
- 返回:
- an array containing the constants of this enum type, in the order they are declared
valueOf
public static TimeUnit valueOf(String name)
- 返回帶有指定名稱的該型別的列舉常量。 字串必須與用於宣告該型別的列舉常量的 識別符號完全匹配。(不允許有多餘 的空格。)
-
- 引數:
指定要返回的列舉常量的名稱。
-- 返回:
- 返回帶有指定名稱的列舉常量
- 丟擲:
如果該列舉型別沒有帶有指定名稱的常量,
- 則丟擲 IllegalArgumentException
convert
public long convert(long sourceDuration, TimeUnit sourceUnit)
- 將給定單元的時間段轉換到此單元。從較細粒度到較粗粒度的舍位轉換,這樣會失去精確性。例如,將 999 毫秒轉換為秒的結果為 0。使用引數從較粗粒度到較細粒度轉換,如果引數為負,則在數字上溢位至 Long.MIN_VALUE,如果為正,則為 Long.MAX_VALUE。
例如,要將 10 分鐘轉換為毫秒,請使用:TimeUnit.MILLISECONDS.convert(10L, TimeUnit.MINUTES)
-
- 引數:
sourceDuration
- 給定 sourceUnit 中的時間段sourceUnit
- sourceDuration 引數的單元- 返回:
- 此單元中的轉換時間段;如果轉換將負溢位,則返回 Long.MIN_VALUE;如果轉換將正溢位,則返回 Long.MAX_VALUE。
toNanos
public long toNanos(long duration)
- 等效於 NANOSECONDS.convert(duration, this)。
-
- 引數:
duration
- 時間段- 返回:
- 轉換時間段,如果轉換將負溢位,則返回 Long.MIN_VALUE;如果轉換將正溢位,則返回 Long.MAX_VALUE。
- 另請參見:
toMicros
public long toMicros(long duration)
- 等效於 MICROSECONDS.convert(duration, this)。
-
- 引數:
duration
- 時間段- 返回:
- 轉換時間段,如果轉換將負溢位,則返回 Long.MIN_VALUE;如果轉換將正溢位,則返回 Long.MAX_VALUE。
- 另請參見:
toMillis
public long toMillis(long duration)
- 等效於 MILLISECONDS.convert(duration, this)。
-
- 引數:
duration
- 時間段- 返回:
- 轉換時間段,如果轉換將負溢位,則返回 Long.MIN_VALUE;如果轉換將正溢位,則返回 Long.MAX_VALUE。
- 另請參見:
toSeconds
public long toSeconds(long duration)
- 等效於 SECONDS.convert(duration, this)。
-
- 引數:
duration
- 時間段- 返回:
- 轉換時間段;如果轉換將負溢位,則返回 Long.MIN_VALUE;如果轉換將正溢位,則返回 Long.MAX_VALUE。
- 另請參見:
toMinutes
public long toMinutes(long duration)
- 等效於 MINUTES.convert(duration, this)。
-
- 引數:
duration
- 時間段- 返回:
- 轉換時間段;如果轉換將負溢位,則返回 Long.MIN_VALUE;如果轉換將正溢位,則返回 Long.MAX_VALUE。
- 從以下版本開始:
- 1.6
- 另請參見:
toHours
public long toHours(long duration)
- 等效於 HOURS.convert(duration, this)。
-
- 引數:
duration
- 時間段- 返回:
- 轉換時間段;如果轉換將負溢位,則返回 Long.MIN_VALUE;如果轉換將正溢位,則返回 Long.MAX_VALUE。
- 從以下版本開始:
- 1.6
- 另請參見:
toDays
public long toDays(long duration)
- 等效於 DAYS.convert(duration, this)。
timedWait
public void timedWait(Object obj, long timeout) throws InterruptedException
- 使用此時間單元執行計時的 Object.wait。這是將超時引數轉換為 Object.wait 方法所需格式的便捷方法。
public synchronized Object poll(long timeout, TimeUnit unit) throws InterruptedException { while (empty) { unit.timedWait(this, timeout); ... } }
-
- 引數:
obj
- 要等待的物件timeout
- 要等待的最長時間。如果小於等於 0,則根本不會等待。- 丟擲:
- 另請參見:
timedJoin
public void timedJoin(Thread thread, long timeout) throws InterruptedException
- 使用此時間單元執行計時的 Thread.join。這是將時間引數轉換為 Thread.join 方法所需格式的便捷方法。
-
- 引數:
thread
- 要等待的執行緒timeout
- 要等待的最長時間。如果小於等於 0,則根本不會等待。- 丟擲:
- 另請參見:
sleep
public void sleep(long timeout) throws InterruptedException
- 使用此單元執行 Thread.sleep.這是將時間引數轉換為 Thread.sleep 方法所需格式的便捷方法。
-
- 引數:
timeout
- 休眠的最短時間。如果小於等於 0,則根本不會休眠。- 丟擲:
- Returns an array containing the constants of this enum type, in the order they are declared. This method may be used to iterate over the constants as follows: