Most visited

Recently visited

Added in API level 1

TimeUnit

public enum TimeUnit
extends Enum<TimeUnit>

java.lang.Object
   ↳ java.lang.Enum<java.util.concurrent.TimeUnit>
     ↳ java.util.concurrent.TimeUnit


A TimeUnit表示给定粒度单位的持续时间,并提供跨设备转换的实用方法,并在这些单元中执行计时和延迟操作。 TimeUnit不保留时间信息,但仅有助于组织和使用时间表示,这些时间表示可以在各种情况下单独维护。 一纳秒被定义为千分之一微秒,一微秒千分之一毫秒,一毫秒千分之一秒,一分钟六十秒,一小时六十分钟,一天二十四小时。

TimeUnit主要用于通知基于时间的方法应该如何解释给定的时间参数。 例如,如果lock不可用,则以下代码将在50毫秒内超时:

 Lock lock = ...;
 if (lock.tryLock(50L, TimeUnit.MILLISECONDS)) ...
while this code will timeout in 50 seconds:
 Lock lock = ...;
 if (lock.tryLock(50L, TimeUnit.SECONDS)) ...
Note however, that there is no guarantee that a particular timeout implementation will be able to notice the passage of time at the same granularity as the given TimeUnit.

Summary

Enum values

TimeUnit  DAYS

时间单位代表二十四小时。

TimeUnit  HOURS

时间单位代表六十分钟。

TimeUnit  MICROSECONDS

时间单位表示千分之一毫秒。

TimeUnit  MILLISECONDS

时间单位代表千分之一秒。

TimeUnit  MINUTES

时间单位代表六十秒。

TimeUnit  NANOSECONDS

时间单位代表千分之一微秒。

TimeUnit  SECONDS

时间单位代表一秒钟。

Public methods

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)

相当于 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 final TimeUnit[] values()

Inherited methods

From class java.lang.Enum
From class java.lang.Object
From interface java.lang.Comparable

Enum values

DAYS

Added in API level 9
TimeUnit DAYS

时间单位代表二十四小时。

HOURS

Added in API level 9
TimeUnit HOURS

时间单位代表六十分钟。

MICROSECONDS

Added in API level 1
TimeUnit MICROSECONDS

时间单位表示千分之一毫秒。

MILLISECONDS

Added in API level 1
TimeUnit MILLISECONDS

时间单位代表千分之一秒。

MINUTES

Added in API level 9
TimeUnit MINUTES

时间单位代表六十秒。

NANOSECONDS

Added in API level 1
TimeUnit NANOSECONDS

时间单位代表千分之一微秒。

SECONDS

Added in API level 1
TimeUnit SECONDS

时间单位代表一秒钟。

Public methods

convert

Added in API level 1
long convert (long sourceDuration, 
                TimeUnit sourceUnit)

将给定单位的给定持续时间转换为本单位。 从较细到较粗粒度的转换会截断,因此会损失精度。 例如,将999毫秒转换为秒数将导致0 从较粗到较细粒度的转换,其参数在数值上会溢出饱和到Long.MIN_VALUE如果为负)或Long.MAX_VALUE如果为正数)。

例如,要将10分钟转换为毫秒,请使用: TimeUnit.MILLISECONDS.convert(10L, TimeUnit.MINUTES)

Parameters
sourceDuration long: the time duration in the given sourceUnit
sourceUnit TimeUnit: the unit of the sourceDuration argument
Returns
long the converted duration in this unit, or Long.MIN_VALUE if conversion would negatively overflow, or Long.MAX_VALUE if it would positively overflow.

sleep

Added in API level 1
void sleep (long timeout)

使用这个时间单位执行Thread.sleep 这是一种方便的方法,它将时间参数转换为Thread.sleep方法所要求的形式。

Parameters
timeout long: the minimum time to sleep. If less than or equal to zero, do not sleep at all.
Throws
InterruptedException if interrupted while sleeping

timedJoin

Added in API level 1
void timedJoin (Thread thread, 
                long timeout)

使用此时间单位执行定时Thread.join 这是一种方便的方法,它将时间参数转换为Thread.join方法所要求的形式。

Parameters
thread Thread: the thread to wait for
timeout long: the maximum time to wait. If less than or equal to zero, do not wait at all.
Throws
InterruptedException if interrupted while waiting

timedWait

Added in API level 1
void timedWait (Object obj, 
                long timeout)

使用此时间单位执行定时Object.wait 这是一种方便的方法,它将超时参数转换为Object.wait方法所要求的形式。

例如,您可以使用 poll方法实现阻止 poll方法(请参阅 BlockingQueue.poll ):

 public synchronized Object poll(long timeout, TimeUnit unit)
     throws InterruptedException {
   while (empty) {
     unit.timedWait(this, timeout);
     ...
   }
 }

Parameters
obj Object: the object to wait on
timeout long: the maximum time to wait. If less than or equal to zero, do not wait at all.
Throws
InterruptedException if interrupted while waiting

toDays

Added in API level 9
long toDays (long duration)

相当于 DAYS.convert(duration, this)

Parameters
duration long: the duration
Returns
long the converted duration

toHours

Added in API level 9
long toHours (long duration)

相当于 HOURS.convert(duration, this)

Parameters
duration long: the duration
Returns
long the converted duration, or Long.MIN_VALUE if conversion would negatively overflow, or Long.MAX_VALUE if it would positively overflow.

toMicros

Added in API level 1
long toMicros (long duration)

相当于 MICROSECONDS.convert(duration, this)

Parameters
duration long: the duration
Returns
long the converted duration, or Long.MIN_VALUE if conversion would negatively overflow, or Long.MAX_VALUE if it would positively overflow.

toMillis

Added in API level 1
long toMillis (long duration)

相当于 MILLISECONDS.convert(duration, this)

Parameters
duration long: the duration
Returns
long the converted duration, or Long.MIN_VALUE if conversion would negatively overflow, or Long.MAX_VALUE if it would positively overflow.

toMinutes

Added in API level 9
long toMinutes (long duration)

相当于 MINUTES.convert(duration, this)

Parameters
duration long: the duration
Returns
long the converted duration, or Long.MIN_VALUE if conversion would negatively overflow, or Long.MAX_VALUE if it would positively overflow.

toNanos

Added in API level 1
long toNanos (long duration)

相当于 NANOSECONDS.convert(duration, this)

Parameters
duration long: the duration
Returns
long the converted duration, or Long.MIN_VALUE if conversion would negatively overflow, or Long.MAX_VALUE if it would positively overflow.

toSeconds

Added in API level 1
long toSeconds (long duration)

相当于 SECONDS.convert(duration, this)

Parameters
duration long: the duration
Returns
long the converted duration, or Long.MIN_VALUE if conversion would negatively overflow, or Long.MAX_VALUE if it would positively overflow.

valueOf

Added in API level 1
TimeUnit valueOf (String name)

Parameters
name String
Returns
TimeUnit

values

Added in API level 1
TimeUnit[] values ()

Returns
TimeUnit[]

Hooray!