java.lang.Objectjava.lang.Enum<TimeUnit>
java.util.concurrent.TimeUnit所有已實現的接口: Serializable,Comparable< TimeUnit>
public enum TimeUnit extends Enum< TimeUnit>TimeUnit 表示給定單元粒度的時間段,它提供在這些單元中進行跨單元轉換和執行計時及延遲操作的實用工具方法。TimeUnit 不維護時間信息,但是有助于組織和使用可能跨各種上下文單獨維護的時間表示形式。毫微秒定義為千分之一微秒,微秒為千分之一毫秒,毫秒為千分之一秒,一分鐘為六十秒,一小時為六十分鐘,一天為二十四小時。
TimeUnit 主要用于通知基于時間的方法如何解釋給定的計時參數。例如,如果
lock
不可用,則以下代碼將在 50 毫秒后超時:Lock lock = ...; if ( lock.tryLock(50L, TimeUnit.MILLISECONDS) ) ... 而以下代碼將在 50 秒后超時:Lock lock = ...; if ( lock.tryLock(50L, TimeUnit.SECONDS) ) ... 但是注意,不保證特定超時實現能夠以與給定 TimeUnit 相同的粒度通知 段。從以下版本開始: 1.5
枚舉常量摘要 | |
---|---|
DAYS | |
HOURS | |
MICROSECONDS | |
MILLISECONDS | |
MINUTES | |
NANOSECONDS | |
SECONDS |
方法摘要 | |
---|---|
long | convert(long sourceDuration,TimeUnit sourceUnit) 將給定單元的時間段轉換到此單元。 |
void | sleep(long timeout) 使用此單元執行 Thread.sleep.這是將時間參數轉換為 Thread.sleep 方法所需格式的便捷方法。 |
void | timedJoin(Thread thread, long timeout) 使用此時間單元執行計時的 Thread.join。 |
void | timedWait(Object obj, long timeout) 使用此時間單元執行計時的 Object.wait。 |
long | toDays(long duration) 等效于 DAYS.convert(duration, this)。 |
long | toHours(long duration) 等效于 HOURS.convert(duration, this)。 |
long | toMicros(long duration) 等效于 MICROSECONDS.convert(duration, this)。 |
long | toMillis(long duration) 等效于 MILLISECONDS.convert(duration, this)。 |
long | toMinutes(long duration) 等效于 MINUTES.convert(duration, this)。 |
long | toNanos(long duration) 等效于 NANOSECONDS.convert(duration, this)。 |
long | toSeconds(long duration) 等效于 SECONDS.convert(duration, this)。 |
static TimeUnit | valueOf(String name) 返回帶有指定名稱的該類型的枚舉常量。 |
static TimeUnit[] | values() Returns an array containing the constants of this enum type, in the order they are declared. |
從類 java.lang.Enum 繼承的方法 |
---|
clone,compareTo, equals, finalize, getDeclaringClass, hashCode, name, ordinal, toString, valueOf |
從類 java.lang.Object 繼承的方法 |
---|
getClass,notify, notifyAll, wait, wait, wait |
枚舉常量詳細信息 |
---|
public static final TimeUnit NANOSECONDSMICROSECONDS
public static final TimeUnit MICROSECONDSMILLISECONDS
public static final TimeUnit MILLISECONDSSECONDS
public static final TimeUnit SECONDSMINUTES
public static final TimeUnit MINUTESHOURS
public static final TimeUnit HOURSDAYS
public static final TimeUnit DAYS
方法詳細信息 |
---|
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 declaredvalueOf
public static TimeUnit valueOf(String name)返回帶有指定名稱的該類型的枚舉常量。 字符串必須與用于聲明該類型的枚舉常量的 標識符 完全匹配。(不允許有多余 的空格。)參數:指定要返回的枚舉常量的名稱。
- 返回: 返回帶有指定名稱的枚舉常量 拋出:如果該枚舉類型沒有帶有指定名稱的常量,
- 則拋出 IllegalArgumentExceptionconvert
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。另請參見:convert(long, java.util.concurrent.TimeUnit)
toMicros
public long toMicros(long duration)等效于 MICROSECONDS.convert(duration, this)。參數:duration
- 時間段 返回: 轉換時間段,如果轉換將負溢出,則返回 Long.MIN_VALUE;如果轉換將正溢出,則返回 Long.MAX_VALUE。另請參見:convert(long, java.util.concurrent.TimeUnit)
toMillis
public long toMillis(long duration)等效于 MILLISECONDS.convert(duration, this)。參數:duration
- 時間段 返回: 轉換時間段,如果轉換將負溢出,則返回 Long.MIN_VALUE;如果轉換將正溢出,則返回 Long.MAX_VALUE。另請參見:convert(long, java.util.concurrent.TimeUnit)
toSeconds
public long toSeconds(long duration)等效于 SECONDS.convert(duration, this)。參數:duration
- 時間段 返回: 轉換時間段;如果轉換將負溢出,則返回 Long.MIN_VALUE;如果轉換將正溢出,則返回 Long.MAX_VALUE。另請參見:convert(long, java.util.concurrent.TimeUnit)
toMinutes
public long toMinutes(long duration)等效于 MINUTES.convert(duration, this)。參數:duration
- 時間段 返回: 轉換時間段;如果轉換將負溢出,則返回 Long.MIN_VALUE;如果轉換將正溢出,則返回 Long.MAX_VALUE。從以下版本開始: 1.6 另請參見:convert(long, java.util.concurrent.TimeUnit)
toHours
public long toHours(long duration)等效于 HOURS.convert(duration, this)。參數:duration
- 時間段 返回: 轉換時間段;如果轉換將負溢出,則返回 Long.MIN_VALUE;如果轉換將正溢出,則返回 Long.MAX_VALUE。從以下版本開始: 1.6 另請參見:convert(long, java.util.concurrent.TimeUnit)
toDays
public long toDays(long duration)等效于 DAYS.convert(duration, this)。參數:duration
- 時間段 返回: 轉換時間段 從以下版本開始: 1.6 另請參見:convert(long, java.util.concurrent.TimeUnit)
timedWait
public void timedWait(Object obj, long timeout) throws InterruptedException使用此時間單元執行計時的 Object.wait。這是將超時參數轉換為 Object.wait 方法所需格式的便捷方法。例如,可以使用以下代碼實現阻塞 poll 方法(參見
BlockingQueue.poll
):public synchronized Object poll(long timeout, TimeUnit unit) throws InterruptedException { while (empty) { unit.timedWait(this, timeout); ... } }參數:obj
- 要等待的對象timeout
- 要等待的最長時間。如果小于等于 0,則根本不會等待。 拋出:InterruptedException
- 如果等待時中斷。另請參見:Object.wait(long, int)
timedJoin
public void timedJoin(Thread thread, long timeout) throws InterruptedException使用此時間單元執行計時的 Thread.join。這是將時間參數轉換為 Thread.join 方法所需格式的便捷方法。參數:thread
- 要等待的線程timeout
- 要等待的最長時間。如果小于等于 0,則根本不會等待。 拋出:InterruptedException
- 如果等待時中斷。另請參見:Thread.join(long, int)
sleep
public void sleep(long timeout) throws InterruptedException使用此單元執行 Thread.sleep.這是將時間參數轉換為 Thread.sleep 方法所需格式的便捷方法。參數:timeout
- 休眠的最短時間。如果小于等于 0,則根本不會休眠。 拋出:InterruptedException
- 如果休眠時中斷。另請參見:Thread.sleep(long)
新聞熱點
疑難解答