Other wait() Methods
Home ] Up ] The wait() Method ] The notify() Method ] [ Other wait() Methods ] The notifyAll() Method ]

 

 

There are also two forms of the wait() method that may be used if the thread is not willing to wait forever:

public final void wait(long timeout) 
    throws InterruptedException

Waits to be notified by another thread of a change in this object. The thread releases ownership of this monitor and waits until either of the following two conditions has occurred:

  • Another thread notifies threads waiting on this object's monitor to wake up either through a call to the notify method or the notifyAll method.
  • The timeout period, specified by the timeout argument in milliseconds, has elapsed. The thread then waits until it can re-obtain ownership of the monitor and resumes execution.

This method should only be called by a thread that is the owner of this object's monitor.

Parameters:

timeout - the maximum time to wait in milliseconds.

This wait() method:

  • Throws IllegalArgumentException if the value of timeout is negative.
  • Throws IllegalMonitorStateException if the current thread is not the owner of the object's monitor.
  • Throws InterruptedException if another thread has interrupted this thread.

public final void wait(long timeout, int nanos) 
    throws InterruptedException

Waits to be notified by another thread of a change in this object. 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 thread releases ownership of this monitor and waits until either of the following two conditions has occurred:

  • Another thread notifies threads waiting on this object's monitor to wake up either through a call to the notify method or the notifyAll method.
  • The timeout period, specified by timeout milliseconds plus nanos nanoseconds arguments, has elapsed.

The thread then waits until it can re-obtain ownership of the monitor and resumes execution This method should only be called by a thread that is the owner of this object's monitor.

Parameters:

timeout - the maximum time to wait in milliseconds.

nanos - additional time, in nanoseconds range 0-999999.

This wait method:

  • Throws IllegalArgumentException if the value of timeout is negative or the value of nanos is not in the range 0-999999.
  • Throws IllegalMonitorStateException if the current thread is not the owner of this object's monitor.
  • Throws InterruptedException if another thread has interrupted this thread.

Throws: IllegalMonitorStateException if the current thread is not the owner of this object's monitor.

 
The page was last updated February 19, 2008