Causes the current thread to wait until another thread invokes the
java.lang.Object.notify()
method or the
java.lang.Object.notifyAll()
method for this object, or
some other thread interrupts the current thread, or a certain
amount of real time has elapsed.
This method is similar to the wait
method of one
argument, but it allows finer control over the amount of time to
wait for a notification before giving up. The amount of real time,
measured in nanoseconds, is given by:
1000000*timeout+nanos
In all other respects, this method does the same thing as the
method wait(long)
of one argument. In particular,
wait(0, 0)
means the same thing as wait(0)
.
The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until either of the following two conditions has occurred:
notify
method
or the notifyAll
method.
timeout
milliseconds plus nanos
nanoseconds arguments, has
elapsed.
The thread then waits until it can re-obtain ownership of the monitor and resumes execution.
As in the one argument version, interrupts and spurious wakeups are possible, and this method should always be used in a loop:
synchronized (obj) { while (<condition does not hold>) obj.wait(timeout, nanos); ... // Perform action appropriate to condition }This method should only be called by a thread that is the owner of this object's monitor. See the
notify
method for a
description of the ways in which a thread can become the owner of
a monitor.
timeout | the maximum time to wait in milliseconds. | |
nanos | additional time, in nanoseconds range 0-999999. |
IllegalArgumentException | if the value of timeout is negative or the value of nanos is not in the range 0-999999. | |
IllegalMonitorStateException | if the current thread is not the owner of this object's monitor. | |
InterruptedException | if any thread interrupted the current thread before or while the current thread was waiting for a notification. The interrupted status of the current thread is cleared when this exception is thrown. |
Diagram: Object