src/share/classes/java/util/concurrent/TimeUnit.java
Print this page
*** 51,66 ****
* how a given timing parameter should be interpreted. For example,
* the following code will timeout in 50 milliseconds if the {@link
* java.util.concurrent.locks.Lock lock} is not available:
*
* <pre> Lock lock = ...;
! * if ( lock.tryLock(50L, TimeUnit.MILLISECONDS) ) ...
* </pre>
* while this code will timeout in 50 seconds:
* <pre>
* Lock lock = ...;
! * if ( lock.tryLock(50L, TimeUnit.SECONDS) ) ...
* </pre>
*
* 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 <tt>TimeUnit</tt>.
--- 51,66 ----
* how a given timing parameter should be interpreted. For example,
* the following code will timeout in 50 milliseconds if the {@link
* java.util.concurrent.locks.Lock lock} is not available:
*
* <pre> Lock lock = ...;
! * if (lock.tryLock(50L, TimeUnit.MILLISECONDS)) ...
* </pre>
* while this code will timeout in 50 seconds:
* <pre>
* Lock lock = ...;
! * if (lock.tryLock(50L, TimeUnit.SECONDS)) ...
* </pre>
*
* 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 <tt>TimeUnit</tt>.
*** 289,318 ****
* @return the number of nanoseconds
*/
abstract int excessNanos(long d, long m);
/**
! * Performs a timed <tt>Object.wait</tt> using this time unit.
* This is a convenience method that converts timeout arguments
* into the form required by the <tt>Object.wait</tt> method.
*
* <p>For example, you could implement a blocking <tt>poll</tt>
* method (see {@link BlockingQueue#poll BlockingQueue.poll})
* using:
*
! * <pre> public synchronized Object poll(long timeout, TimeUnit unit) throws InterruptedException {
* while (empty) {
* unit.timedWait(this, timeout);
* ...
* }
! * }</pre>
*
* @param obj the object to wait on
* @param 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 Object#wait(long, int)
*/
public void timedWait(Object obj, long timeout)
throws InterruptedException {
if (timeout > 0) {
long ms = toMillis(timeout);
--- 289,320 ----
* @return the number of nanoseconds
*/
abstract int excessNanos(long d, long m);
/**
! * Performs a timed {@link Object#wait(long, int) Object.wait}
! * using this time unit.
* This is a convenience method that converts timeout arguments
* into the form required by the <tt>Object.wait</tt> method.
*
* <p>For example, you could implement a blocking <tt>poll</tt>
* method (see {@link BlockingQueue#poll BlockingQueue.poll})
* using:
*
! * <pre> {@code
! * public synchronized Object poll(long timeout, TimeUnit unit)
! * throws InterruptedException {
* while (empty) {
* unit.timedWait(this, timeout);
* ...
* }
! * }}</pre>
*
* @param obj the object to wait on
* @param timeout the maximum time to wait. If less than
* or equal to zero, do not wait at all.
! * @throws InterruptedException if interrupted while waiting
*/
public void timedWait(Object obj, long timeout)
throws InterruptedException {
if (timeout > 0) {
long ms = toMillis(timeout);
*** 320,337 ****
obj.wait(ms, ns);
}
}
/**
! * Performs a timed <tt>Thread.join</tt> using this time unit.
* This is a convenience method that converts time arguments into the
* form required by the <tt>Thread.join</tt> method.
* @param thread the thread to wait for
* @param 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 Thread#join(long, int)
*/
public void timedJoin(Thread thread, long timeout)
throws InterruptedException {
if (timeout > 0) {
long ms = toMillis(timeout);
--- 322,340 ----
obj.wait(ms, ns);
}
}
/**
! * Performs a timed {@link Thread#join(long, int) Thread.join}
! * using this time unit.
* This is a convenience method that converts time arguments into the
* form required by the <tt>Thread.join</tt> method.
+ *
* @param thread the thread to wait for
* @param timeout the maximum time to wait. If less than
* or equal to zero, do not wait at all.
! * @throws InterruptedException if interrupted while waiting
*/
public void timedJoin(Thread thread, long timeout)
throws InterruptedException {
if (timeout > 0) {
long ms = toMillis(timeout);
*** 339,355 ****
thread.join(ms, ns);
}
}
/**
! * Performs a <tt>Thread.sleep</tt> using this unit.
* This is a convenience method that converts time arguments into the
* form required by the <tt>Thread.sleep</tt> method.
* @param timeout the minimum time to sleep. If less than
* or equal to zero, do not sleep at all.
! * @throws InterruptedException if interrupted while sleeping.
! * @see Thread#sleep
*/
public void sleep(long timeout) throws InterruptedException {
if (timeout > 0) {
long ms = toMillis(timeout);
int ns = excessNanos(timeout, ms);
--- 342,359 ----
thread.join(ms, ns);
}
}
/**
! * Performs a {@link Thread#sleep(long, int) Thread.sleep} using
! * this time unit.
* This is a convenience method that converts time arguments into the
* form required by the <tt>Thread.sleep</tt> method.
+ *
* @param timeout the minimum time to sleep. If less than
* or equal to zero, do not sleep at all.
! * @throws InterruptedException if interrupted while sleeping
*/
public void sleep(long timeout) throws InterruptedException {
if (timeout > 0) {
long ms = toMillis(timeout);
int ns = excessNanos(timeout, ms);