aj983
aj983

Reputation: 293

Wait method confusion

I have a confusion with wait() method of thread class. It is known that wait() method is called from a synchronized context always. But after execution of wait method, will that thread release the lock on that object which it was holding.

I mean to ask that "When a thread goes to wait pool of an object, before going will it release the lock it has."

Upvotes: 0

Views: 110

Answers (2)

Brian Roach
Brian Roach

Reputation: 76918

http://download.oracle.com/javase/6/docs/api/java/lang/Object.html#wait()

Causes current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object.

And to answer your second question: never. As in your last question, locks aren't ever arbitrarily released - that would break synchronization.

Oracle provides a fairly detailed tutorial that covers all of this information:

http://download.oracle.com/javase/tutorial/essential/concurrency/

Upvotes: 0

Eric Rosenberg
Eric Rosenberg

Reputation: 1533

yes. Its pretty clearly stated in the javadoc: wait

http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Object.html#wait()

public final void wait() throws InterruptedException

Causes current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object. In other words, this method behaves exactly as if it simply performs the call wait(0). The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until 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 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. See the notify method for a description of the ways in which a thread can become the owner of a monitor.

Throws: IllegalMonitorStateException - if the current thread is not the owner of the object's monitor. InterruptedException - if another thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown. See Also: notify(), notifyAll()

Upvotes: 1

Related Questions