public enum TimeUnit extends Enum<TimeUnit>
TimeUnit
代表时间在给定单位的粒度,提供实用的方法来转换过的单位,并进行定时和延时操作这些单位。一个
TimeUnit
不保持的时间信息,但只有帮助组织和使用时间表示可单独保存在各种环境中。纳秒级定义为一千分之一微秒,微秒的一千分之一毫秒,毫秒为一千分之一秒,一分钟六十秒,一小时是六十分钟,和二零四小时的一天。
一个TimeUnit
主要用来通知时间的方法如何给定时参数的解释应。例如,下面的代码将超时在50毫秒内如果lock
不可用:
Lock lock = ...;
if (lock.tryLock(50L, TimeUnit.MILLISECONDS)) ...
,此代码将超时50秒
Lock lock = ...;
if (lock.tryLock(50L, TimeUnit.SECONDS)) ...
注但没有保证某一特定超时的实现可以在粒度为给
TimeUnit
同样注意到时间的流逝。
Enum Constant and Description |
---|
DAYS
代表二零四小时的时间单位
|
HOURS
代表六十分钟的时间单位
|
MICROSECONDS
代表毫秒的一千分之一的时间单位
|
MILLISECONDS
代表第二个的一千分之一的时间单位
|
MINUTES
代表六十秒的时间单位
|
NANOSECONDS
时间单位代表一千分之一微秒
|
SECONDS
代表一秒的时间单位
|
Modifier and Type | Method and Description |
---|---|
long |
convert(long sourceDuration, TimeUnit sourceUnit)
将给定单位中的给定时间持续时间转换为该单元。
|
void |
sleep(long timeout)
执行
Thread.sleep 利用这个时间单位。
|
void |
timedJoin(Thread thread, long timeout)
执行定时
Thread.join 利用这个时间单位。
|
void |
timedWait(Object obj, long timeout)
执行定时
Object.wait 利用这个时间单位。
|
long |
toDays(long duration)
|
long |
toHours(long duration)
|
long |
toMicros(long duration)
|
long |
toMillis(long duration)
|
long |
toMinutes(long duration)
|
long |
toNanos(long duration)
|
long |
toSeconds(long duration)
|
static TimeUnit |
valueOf(String name)
返回此类型具有指定名称的枚举常量。
|
static TimeUnit[] |
values()
返回一个数组包含该枚举类型的常量,它们的顺序声明。
|
public static final TimeUnit NANOSECONDS
public static final TimeUnit MICROSECONDS
public static final TimeUnit MILLISECONDS
public static final TimeUnit SECONDS
public static final TimeUnit MINUTES
public static final TimeUnit HOURS
public static final TimeUnit DAYS
public static TimeUnit[] values()
对于(timeunit C:timeunit。values()) 系统,println(C);
public static TimeUnit valueOf(String name)
name
-定要返回的枚举的名称。
IllegalArgumentException
-如果这个枚举类型,也没有固定的具有指定名称
NullPointerException
-如果参数为空
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
是否正溢出。
public long toNanos(long duration)
duration
-时间
Long.MIN_VALUE
如果转换负溢出,或
Long.MAX_VALUE
是否正溢出。
public long toMicros(long duration)
duration
-时间
Long.MIN_VALUE
如果转换负溢出,或
Long.MAX_VALUE
是否正溢出。
public long toMillis(long duration)
duration
-时间
Long.MIN_VALUE
如果转换负溢出,或
Long.MAX_VALUE
是否正溢出。
public long toSeconds(long duration)
duration
-时间
Long.MIN_VALUE
如果转换负溢出,或
Long.MAX_VALUE
是否正溢出。
public long toMinutes(long duration)
duration
-时间
Long.MIN_VALUE
如果转换负溢出,或
Long.MAX_VALUE
是否正溢出。
public long toHours(long duration)
duration
-时间
Long.MIN_VALUE
如果转换负溢出,或
Long.MAX_VALUE
是否正溢出。
public long toDays(long duration)
duration
-时间
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
如果中断等待
public void timedJoin(Thread thread, long timeout) throws InterruptedException
Thread.join
利用这个时间单位。这是一个方便的方法,将时间参数的
Thread.join
方法所要求的形式。
thread
-线程等待
timeout
-最大等待时间。如果小于或等于零,就不要再等了。
InterruptedException
如果中断等待
public void sleep(long timeout) throws InterruptedException
Thread.sleep
利用这个时间单位。这是一个方便的方法,将时间参数的
Thread.sleep
方法所要求的形式。
timeout
睡觉时间最小。如果小于或等于零,根本不睡觉。
InterruptedException
如果中断而睡
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.