|
FlexDoc/Javadoc 2.0 Demo Java Doc |
A TimeUnit is mainly used to inform time-based methods how a given timing parameter should be interpreted. For example, the following code will timeout in 50 milliseconds if the lock is not available:
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.Nested Class Summary |
Nested classes/interfaces inherited from class java.lang.Enum |
Enum.EnumDesc<E extends Enum<E>> |
Enum Constant Summary |
|
Time unit representing twenty four hours.
|
|
Time unit representing sixty minutes.
|
|
Time unit representing one thousandth of a millisecond.
|
|
Time unit representing one thousandth of a second.
|
|
Time unit representing sixty seconds.
|
|
Time unit representing one thousandth of a microsecond.
|
|
Time unit representing one second.
|
Method Summary |
||
long |
Converts the given time duration to this unit.
|
|
long |
Converts the given time duration in the given unit to this unit.
|
|
static TimeUnit |
of(ChronoUnit chronoUnit)
Converts a ChronoUnit to the equivalent TimeUnit.
|
|
void |
sleep(long timeout)
Performs a Thread.sleep using
this time unit.
|
|
void |
Performs a timed Thread.join
using this time unit.
|
|
void |
Performs a timed Object.wait
using this time unit.
|
|
Converts this TimeUnit to the equivalent ChronoUnit.
|
||
long |
toDays(long duration)
Equivalent to
DAYS.convert(duration, this).
|
|
long |
toHours(long duration)
Equivalent to
HOURS.convert(duration, this).
|
|
long |
toMicros(long duration)
Equivalent to
MICROSECONDS.convert(duration, this).
|
|
long |
toMillis(long duration)
Equivalent to
MILLISECONDS.convert(duration, this).
|
|
long |
toMinutes(long duration)
Equivalent to
MINUTES.convert(duration, this).
|
|
long |
toNanos(long duration)
Equivalent to
NANOSECONDS.convert(duration, this).
|
|
long |
toSeconds(long duration)
Equivalent to
SECONDS.convert(duration, this).
|
|
static TimeUnit |
||
static TimeUnit[] |
values()
|
Methods inherited from class java.lang.Enum |
Methods inherited from class java.lang.Object |
static TimeUnit[] values |
() |
static TimeUnit valueOf |
(String name) |
long convert |
(long sourceDuration, TimeUnit sourceUnit) |
For example, to convert 10 minutes to milliseconds, use: TimeUnit.MILLISECONDS.convert(10L, TimeUnit.MINUTES)
long convert |
(Duration duration) |
For any TimeUnit unit, unit.convert(Duration.ofNanos(n)) is equivalent to unit.convert(n, NANOSECONDS), and unit.convert(Duration.of(n, unit.toChronoUnit())) is equivalent to n (in the absence of overflow).
long toNanos |
(long duration) |
long toMicros |
(long duration) |
long toMillis |
(long duration) |
long toSeconds |
(long duration) |
long toMinutes |
(long duration) |
long toHours |
(long duration) |
long toDays |
(long duration) |
void timedWait |
(Object obj, long timeout) |
throws |
For example, you could implement a blocking poll method (see BlockingQueue.poll) using:
public E poll(long timeout, TimeUnit unit)
throws InterruptedException {
synchronized (lock) {
while (isEmpty()) {
unit.timedWait(lock, timeout);
...
}
}
}
void timedJoin |
(Thread thread, long timeout) |
throws |
void sleep |
(long timeout) |
throws |
ChronoUnit toChronoUnit |
() |
static TimeUnit of |
(ChronoUnit chronoUnit) |
|
FlexDoc/Javadoc 2.0 Demo Java Doc |