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

首頁 > 學院 > 開發(fā)設計 > 正文

枚舉 TimeUnit 使用和說明

2019-11-09 19:52:40
字體:
來源:轉載
供稿:網友

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

java.util.concurrent 枚舉 TimeUnit

java.lang.Object  繼承者 java.lang.Enum<TimeUnit>      繼承者 java.util.concurrent.TimeUnit所有已實現(xiàn)的接口: Serializable,Comparable< TimeUnit> 
public enum TimeUnit           extends       Enum<      TimeUnit>     

TimeUnit 表示給定單元粒度的時間段,它提供在這些單元中進行跨單元轉換和執(zhí)行計時及延遲操作的實用工具方法。TimeUnit 不維護時間信息,但是有助于組織和使用可能跨各種上下文單獨維護的時間表示形式。毫微秒定義為千分之一微秒,微秒為千分之一毫秒,毫秒為千分之一秒,一分鐘為六十秒,一小時為六十分鐘,一天為二十四小時。

TimeUnit 主要用于通知基于時間的方法如何解釋給定的計時參數。例如,如果 lock 不可用,則以下代碼將在 50 毫秒后超時:

  Lock lock = ...;  if ( lock.tryLock(50L, TimeUnit.MILLISECONDS) ) ... 而以下代碼將在 50 秒后超時:
  Lock lock = ...;  if ( lock.tryLock(50L, TimeUnit.SECONDS) ) ... 但是注意,不保證特定超時實現(xiàn)能夠以與給定 TimeUnit 相同的粒度通知 段。

從以下版本開始: 1.5

枚舉常量摘要
DAYS           
HOURS           
MICROSECONDS           
MILLISECONDS           
MINUTES           
NANOSECONDS           
SECONDS           
 
方法摘要
 longconvert(long sourceDuration,TimeUnit sourceUnit)           將給定單元的時間段轉換到此單元。
 voidsleep(long timeout)          使用此單元執(zhí)行 Thread.sleep.這是將時間參數轉換為 Thread.sleep 方法所需格式的便捷方法。
 voidtimedJoin(Thread thread, long timeout)           使用此時間單元執(zhí)行計時的 Thread.join。
 voidtimedWait(Object obj, long timeout)           使用此時間單元執(zhí)行計時的 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使用此時間單元執(zhí)行計時的 Object.wait。這是將超時參數轉換為 Object.wait 方法所需格式的便捷方法。

例如,可以使用以下代碼實現(xiàn)阻塞 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使用此時間單元執(zhí)行計時的 Thread.join。這是將時間參數轉換為 Thread.join 方法所需格式的便捷方法。

參數: thread - 要等待的線程 timeout - 要等待的最長時間。如果小于等于 0,則根本不會等待。 拋出: InterruptedException - 如果等待時中斷。另請參見: Thread.join(long, int)

sleep

public void sleep(long timeout)           throws InterruptedException使用此單元執(zhí)行 Thread.sleep.這是將時間參數轉換為 Thread.sleep 方法所需格式的便捷方法。

參數: timeout - 休眠的最短時間。如果小于等于 0,則根本不會休眠。 拋出: InterruptedException - 如果休眠時中斷。另請參見: Thread.sleep(long)
上一篇:理解 R summary

下一篇:C#復選框

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 海南省| 当阳市| 阿图什市| 昭平县| 庐江县| 道真| 济南市| 息烽县| 台东市| 高州市| 长葛市| 建阳市| 连平县| 阳谷县| 西丰县| 扬中市| 青浦区| 天气| 随州市| 富阳市| 铅山县| 区。| 南雄市| 泸西县| 清河县| 卢湾区| 湛江市| 蕲春县| 绿春县| 新津县| 阳江市| 京山县| 临武县| 巴塘县| 新化县| 汝阳县| 荣昌县| 鄂托克前旗| 枣强县| 怀化市| 辰溪县|