wait()method on an object?
(Assume that the calling Thread has the lock of that object.)
[Select 1 option: ]
- The thread that has called the
wait()method will not come out of the WAITING state until some other thread calls
notifyAll()on the same object or interrupts the thread.
- The object issuing the call to
wait()will halt until another object sends a
- An exception might be raised if it is not called in a synchronized block otherwise no exception will be raised.
- The thread calling wait() will be automatically synchronized with any other threads who call wait().
- None of the above
b. Note that it is not the object that calls the wait/notify but the thread.
c. It will ALWAYS raise IllegalMonitorStateException if not called in a synchronized block for that object.
d. Only if all the threads call wait on the same object.
A Thread should have the monitor of that object before calling wait() on it. So if a thread synchronizes on some object and calls
wait()on another object, then it will throw an IllegalMonitorStateException. Once a thread calls
wait()on an object, it will wait until this thread is asked to come out of the wait state. This happens when any of the following happens.
- the time in milliseconds passed in as a parameter to wait() elapses.
- some other thread calls notifyAll on the same object.
- some other thread calls notify on the same object AND the JVM picks this particular thread (out of all the threads that might have called wait() on the same object) to wake up.
- some other thread calls this thread's interrupt method. In all the above cases, the waiting thread comes out of the WAITING state but goes into BLOCKED state. It must still reacquire the lock before continuing further (because it is still within the synchronized block).