- java.lang.Object
-
- java.lang.Enum<TimeUnit>
-
- java.util.concurrent.TimeUnit
-
- All Implemented Interfaces:
-
Serializable,Comparable<TimeUnit>
public enum TimeUnit extends Enum<TimeUnit>
ATimeUnit表示给定的粒度单位的持续时间,并提供了跨单位转换的实用方法,并在这些单元中执行定时和延迟操作。 ATimeUnit不保留时间信息,但仅有助于组织和使用可能在各种上下文中单独维护的时间表示。 一纳秒定义为千分之一秒,微秒为千分之一毫秒,毫秒为千分之一秒,一分钟为六十秒,一小时为六十分钟,一天为二十四小时。A
TimeUnit主要用于通知基于时间的方法如何解释给定的时序参数。 例如,如果lock不可用,以下代码将在50毫秒内超时:而此代码将在50秒内超时:Lock lock = ...; if (lock.tryLock(50L, TimeUnit.MILLISECONDS)) ...然而,请注意,不能保证特定的超时执行将能够以与给定的Lock lock = ...; if (lock.tryLock(50L, TimeUnit.SECONDS)) ...TimeUnit相同的粒度注意到时间的TimeUnit。- 从以下版本开始:
- 1.5
-
-
Enum Constant Summary
Enum Constants Enum Constant 描述 DAYS时间单位代表二十四小时。HOURS时间单位代表六十分钟。MICROSECONDS时间单位代表千分之一毫秒。MILLISECONDS时间单位为千分之一秒。MINUTES时间单位代表60秒。NANOSECONDS时间单位代表千分之一千分之一。SECONDS时间单位代表一秒。
-
方法摘要
所有方法 静态方法 接口方法 具体的方法 Modifier and Type 方法 描述 longconvert(long sourceDuration, TimeUnit sourceUnit)将给定单位的给定持续时间转换为本机。static TimeUnitof(ChronoUnit chronoUnit)将ChronoUnit转换为等效的TimeUnit。voidsleep(long timeout)使用此时间单位执行Thread.sleep。voidtimedJoin(Thread thread, long timeout)使用此时间单位执行定时Thread.join。voidtimedWait(Object obj, long timeout)使用此时间单位执行定时Object.wait。ChronoUnittoChronoUnit()将此TimeUnit转换为等效的ChronoUnit。longtoDays(long duration)longtoHours(long duration)longtoMicros(long duration)longtoMillis(long duration)longtoMinutes(long duration)longtoNanos(long duration)longtoSeconds(long duration)static TimeUnitvalueOf(String name)以指定的名称返回此类型的枚举常量。static TimeUnit[]values()按照它们声明的顺序返回一个包含此枚举类型常量的数组。
-
-
-
Enum Constant Detail
-
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
时间单位代表60秒。- 从以下版本开始:
- 1.6
-
HOURS
public static final TimeUnit HOURS
时间单位代表六十分钟。- 从以下版本开始:
- 1.6
-
DAYS
public static final TimeUnit DAYS
时间单位代表二十四小时。- 从以下版本开始:
- 1.6
-
-
方法详细信息
-
values
public static TimeUnit[] values()
按照它们声明的顺序返回一个包含此枚举类型常量的数组。 该方法可用于遍历常量如下:for (TimeUnit c : TimeUnit.values()) System.out.println(c);- 结果
- 一个包含这个枚举类型的常量的数组,按照它们被声明的顺序
-
valueOf
public static TimeUnit valueOf(String name)
以指定的名称返回此类型的枚举常量。 字符串必须完全匹配用于声明此类型的枚举常量的标识符。 (不允许使用外来空白字符。)- 参数
-
name- 要返回的枚举常数的名称。 - 结果
- 具有指定名称的枚举常数
- 异常
-
IllegalArgumentException- 如果此枚举类型没有指定名称的常量 -
NullPointerException- 如果参数为null
-
convert
public long convert(long sourceDuration, TimeUnit sourceUnit)将给定单位的给定持续时间转换为本机。 从更精细到更粗糙的粒度的转换截断,因此失去精度。 例如,将999毫秒转换为秒会导致0。 从更粗糙到更细粒度的转换,数值溢出饱和的Long.MIN_VALUE如果为负,Long.MAX_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)
- 参数
-
duration- 持续时间 - 结果
-
转换的持续时间,或
Long.MIN_VALUE如果转换将负面溢出,或Long.MAX_VALUE如果它会积极溢出。
-
toMicros
public long toMicros(long duration)
- 参数
-
duration- 持续时间 - 结果
-
转换的持续时间,或
Long.MIN_VALUE如果转换将负面溢出,或Long.MAX_VALUE如果它会积极溢出。
-
toMillis
public long toMillis(long duration)
- 参数
-
duration- 持续时间 - 结果
-
转换的持续时间,或
Long.MIN_VALUE如果转换将负面溢出,或Long.MAX_VALUE如果它会积极的溢出。
-
toSeconds
public long toSeconds(long duration)
- 参数
-
duration- 持续时间 - 结果
-
转换的持续时间,或
Long.MIN_VALUE如果转换将负面溢出,或Long.MAX_VALUE如果它会积极溢出。
-
toMinutes
public long toMinutes(long duration)
- 参数
-
duration- 持续时间 - 结果
-
转换的持续时间,或
Long.MIN_VALUE如果转换将负面溢出,或Long.MAX_VALUE如果它会积极溢出。 - 从以下版本开始:
- 1.6
-
toHours
public long toHours(long duration)
- 参数
-
duration- 持续时间 - 结果
-
转换的持续时间,或
Long.MIN_VALUE如果转换将负面溢出,或Long.MAX_VALUE如果它会积极溢出。 - 从以下版本开始:
- 1.6
-
toDays
public long toDays(long duration)
- 参数
-
duration- 持续时间 - 结果
- 转换的持续时间
- 从以下版本开始:
- 1.6
-
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- 等待的最长时间。 如果小于或等于零,不要等待。 - 异常
-
InterruptedException- 如果在等待时中断
-
timedJoin
public void timedJoin(Thread thread, long timeout) throws InterruptedException
使用此时间单位执行定时Thread.join。 这是一种方便的方法,将时间参数转换为Thread.join方法所需的形式。- 参数
-
thread- 等待的线程 -
timeout- 等待的最长时间。 如果小于或等于零,不要等待。 - 异常
-
InterruptedException- 如果在等待时中断
-
sleep
public void sleep(long timeout) throws InterruptedException使用此时间单位执行Thread.sleep。 这是一种方便的方法,将时间参数转换为Thread.sleep方法所需的形式。- 参数
-
timeout- 睡眠时间最短 如果小于或等于零,不要睡觉。 - 异常
-
InterruptedException- 睡觉时中断
-
toChronoUnit
public ChronoUnit toChronoUnit()
将此TimeUnit转换为等效的ChronoUnit。- 结果
- 转换后的等效ChronoUnit
- 从以下版本开始:
- 9
-
of
public static TimeUnit of(ChronoUnit chronoUnit)
将ChronoUnit转换为等效的TimeUnit。- 参数
-
chronoUnit- 要转换的ChronoUnit - 结果
- 转换的等效TimeUnit
- 异常
-
IllegalArgumentException- 如果chronoUnit没有等效的TimeUnit -
NullPointerException- 如果chronoUnit为空 - 从以下版本开始:
- 9
-
-