国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

枚舉 TimeUnit 使用和說明

2019-11-10 17:36:58
字體:
來源:轉載
供稿:網友

long millis = TimeUnit.NANOSECONDS.toMillis(long duration);

java.util.concurrent 枚舉 TimeUnit

java.lang.Object  繼承者 java.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           
 
方法摘要
 longconvert(long sourceDuration,TimeUnit sourceUnit)           將給定單元的時間段轉換到此單元。
 voidsleep(long timeout)          使用此單元執行 Thread.sleep.這是將時間參數轉換為 Thread.sleep 方法所需格式的便捷方法。
 voidtimedJoin(Thread thread, long timeout)           使用此時間單元執行計時的 Thread.join
 voidtimedWait(Object obj, long timeout)           使用此時間單元執行計時的 Object.wait
 longtoDays(long duration)          等效于 DAYS.convert(duration, this)
 longtoHours(long duration)          等效于 HOURS.convert(duration, this)
 longtoMicros(long duration)          等效于 MICROSECONDS.convert(duration, this)
 longtoMillis(long duration)          等效于 MILLISECONDS.convert(duration, this)
 longtoMinutes(long duration)          等效于 MINUTES.convert(duration, this)
 longtoNanos(long duration)          等效于 NANOSECONDS.convert(duration, this)
 longtoSeconds(long duration)          等效于 SECONDS.convert(duration, this)
static TimeUnitvalueOf(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
 

枚舉常量詳細信息

NANOSECONDS

public static final TimeUnit NANOSECONDS

MICROSECONDS

public static final TimeUnit MICROSECONDS

MILLISECONDS

public static final TimeUnit MILLISECONDS

SECONDS

public static final TimeUnit SECONDS

MINUTES

public static final TimeUnit MINUTES

HOURS

public static final TimeUnit HOURS

DAYS

public static final TimeUnit DAYS
方法詳細信息

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另請參見: 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)
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 清水县| 革吉县| 伊宁市| 梁山县| 中西区| 天柱县| 崇礼县| 青州市| 衡南县| 丘北县| 虹口区| 安泽县| 九江县| 上饶市| 大方县| 高雄市| 延庆县| 大埔区| 犍为县| 临安市| 乐东| 琼结县| 托里县| 望奎县| 揭西县| 新宾| 宿松县| 轮台县| 毕节市| 海宁市| 乌拉特后旗| 周口市| 临清市| 中阳县| 米林县| 永康市| 勐海县| 洪湖市| 洪湖市| 柏乡县| 乐亭县|