edu.emory.mathcs.backport.java.util.concurrent
public abstract class TimeUnit extends Object implements Serializable
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.
Since: 1.5
| Field Summary | |
|---|---|
| static TimeUnit | DAYS |
| static TimeUnit | HOURS |
| static TimeUnit | MICROSECONDS |
| static TimeUnit | MILLISECONDS |
| static TimeUnit | MINUTES |
| static TimeUnit | NANOSECONDS |
| static TimeUnit | SECONDS |
| Method Summary | |
|---|---|
| abstract long | convert(long sourceDuration, TimeUnit sourceUnit)
Convert the given time duration in the given unit to this
unit. |
| String | name()
Returns the name of this enum constant, exactly as declared in its enum
declaration. |
| int | ordinal()
Returns the ordinal of this enumeration constant (its position in its
enum declaration, where the initial constant is assigned an ordinal of
zero). |
| protected Object | readResolve() |
| void | sleep(long timeout)
Performs a Thread.sleep using this unit.
|
| void | timedJoin(Thread thread, long timeout)
Performs a timed Thread.join using this time unit.
|
| void | timedWait(Object obj, long timeout)
Performs a timed Object.wait using this time unit.
|
| abstract long | toDays(long duration)
Equivalent to DAYS.convert(duration, this). |
| abstract long | toHours(long duration)
Equivalent to HOURS.convert(duration, this). |
| abstract long | toMicros(long duration)
Equivalent to MICROSECONDS.convert(duration, this). |
| abstract long | toMillis(long duration)
Equivalent to MILLISECONDS.convert(duration, this). |
| abstract long | toMinutes(long duration)
Equivalent to MINUTES.convert(duration, this). |
| abstract long | toNanos(long duration)
Equivalent to NANOSECONDS.convert(duration, this). |
| abstract long | toSeconds(long duration)
Equivalent to SECONDS.convert(duration, this). |
| String | toString() |
| static TimeUnit | valueOf(String name)
Returns the enum constant of this type with the specified name. |
| static TimeUnit[] | values() |
For example, to convert 10 minutes to milliseconds, use: TimeUnit.MILLISECONDS.convert(10L, TimeUnit.MINUTES)
Parameters: sourceDuration the time duration in the given sourceUnit sourceUnit the unit of the sourceDuration argument
Returns: the converted duration in this unit, or Long.MIN_VALUE if conversion would negatively overflow, or Long.MAX_VALUE if it would positively overflow.
Returns: the name of this enum constant
EnumSet and EnumMap.
Returns: the ordinal of this enumeration constant
Parameters: timeout the maximum time to sleep. If less than or equal to zero, do not sleep at all.
Throws: InterruptedException if interrupted while sleeping.
See Also: java.lang.Thread#sleep
Parameters: thread the thread to wait for timeout the maximum time to wait. If less than or equal to zero, do not wait at all.
Throws: InterruptedException if interrupted while waiting.
See Also: java.lang.Thread#join(long, int)
For example, you could implement a blocking poll
method (see BlockingQueue.poll)
using:
public synchronized Object poll(long timeout, TimeUnit unit) throws InterruptedException {
while (empty) {
unit.timedWait(this, timeout);
...
}
}
Parameters: obj the object to wait on timeout the maximum time to wait. If less than or equal to zero, do not wait at all.
Throws: InterruptedException if interrupted while waiting.
See Also: java.lang.Object#wait(long, int)
Parameters: duration the duration
Returns: the converted duration
Since: 1.6
See Also: TimeUnit
Parameters: duration the duration
Returns: the converted duration, or Long.MIN_VALUE if conversion would negatively overflow, or Long.MAX_VALUE if it would positively overflow.
Since: 1.6
See Also: TimeUnit
Parameters: duration the duration
Returns: the converted duration, or Long.MIN_VALUE if conversion would negatively overflow, or Long.MAX_VALUE if it would positively overflow.
See Also: TimeUnit
Parameters: duration the duration
Returns: the converted duration, or Long.MIN_VALUE if conversion would negatively overflow, or Long.MAX_VALUE if it would positively overflow.
See Also: TimeUnit
Parameters: duration the duration
Returns: the converted duration, or Long.MIN_VALUE if conversion would negatively overflow, or Long.MAX_VALUE if it would positively overflow.
Since: 1.6
See Also: TimeUnit
Parameters: duration the duration
Returns: the converted duration, or Long.MIN_VALUE if conversion would negatively overflow, or Long.MAX_VALUE if it would positively overflow.
See Also: TimeUnit
Parameters: duration the duration
Returns: the converted duration, or Long.MIN_VALUE if conversion would negatively overflow, or Long.MAX_VALUE if it would positively overflow.
See Also: TimeUnit
Parameters: name the name of the enum constant to be returned
Returns: the enum constant with the specified name
Throws: IllegalArgumentException if this enum type has no constant with the specified name