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.
| Enum Constant Summary |
|---|
|
No description provided. |
|
No description provided. |
|
No description provided. |
|
No description provided. |
|
No description provided. |
|
No description provided. |
|
No description provided. |
| Method Summary | |
|---|---|
| long |
Convert the given time duration in the given unit to this unit. |
| void |
Performs a Thread.sleep using this unit. |
| void |
Performs a timed Thread.join using this time unit. |
| void |
Performs a timed Object.wait using this time unit. |
| long |
Equivalent to DAYS.convert(duration, this). |
| long |
Equivalent to HOURS.convert(duration, this). |
| long |
Equivalent to MICROSECONDS.convert(duration, this). |
| long |
Equivalent to MILLISECONDS.convert(duration, this). |
| long |
Equivalent to MINUTES.convert(duration, this). |
| long |
Equivalent to NANOSECONDS.convert(duration, this). |
| long |
Equivalent to SECONDS.convert(duration, this). |
| static TimeUnit |
No description provided. |
| static TimeUnit[] |
No description provided. |
For example, to convert 10 minutes to milliseconds, use: TimeUnit.MILLISECONDS.convert(10L, TimeUnit.MINUTES)
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);
...
}
}
public
long
toDays
(
long
duration
)
public
long
toHours
(
long
duration
)
public
long
toMicros
(
long
duration
)
public
long
toMillis
(
long
duration
)
public
long
toMinutes
(
long
duration
)
public
long
toNanos
(
long
duration
)
public
long
toSeconds
(
long
duration
)