Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java : Does wait() release lock from synchronized block

I was under the impression that wait() releases all locks but I found this post which says

"Invoking wait inside a synchronized method is a simple way to acquire the intrinsic lock"

Please clarify I'm a bit confused.

http://docs.oracle.com/javase/tutorial/essential/concurrency/guardmeth.html

like image 654
Abhijit Avatar asked Nov 06 '12 11:11

Abhijit


People also ask

What happens if sleep () and wait () executes in synchronized block?

Difference between wait() and sleep()wait() is used for inter-thread communication while sleep() is used to introduce a pause on execution, generally. wait() should be called from inside synchronise or else we get an IllegalMonitorStateException , while sleep() can be called anywhere.

Why must wait () always be in synchronized block?

As Michael Borgwardt points out, wait/notify is all about communication between threads, so you'll always end up with a race condition similar to the one described above. This is why the "only wait inside synchronized" rule is enforced.

Is it possible to call wait () in a non synchronized block?

If you need to call wait(), notify(), or notifyAll() from within a non-synchronized method, then you must first obtain a lock on the object's monitor. If you don't, an exception will be generated when an attempt is made to call the method in question.

Why wait () notify () and notifyAll () methods have to be called from a synchronized method or block?

Calling notify() or notifyAll() methods issues a notification to a single or multiple threads that a condition has changed and once the notification thread leaves the synchronized block, all the threads which are waiting for fight for object lock on which they are waiting and lucky thread returns from wait() method ...


1 Answers

"Invoking wait inside a synchronized method is a simple way to acquire the intrinsic lock"

This sentence is false, it is an error in documentation.

Thread acquires the intrinsic lock when it enters a synchronized method. Thread inside the synchronized method is set as the owner of the lock and is in RUNNABLE state. Any thread that attempts to enter the locked method becomes BLOCKED.

When thread calls wait it releases the current object lock (it keeps all locks from other objects) and than goes to WAITING state.

When some other thread calls notify or notifyAll on that same object the first thread changes state from WAITING to BLOCKED, Notified thread does NOT automatically reacquire the lock or become RUNNABLE, in fact it must fight for the lock with all other blocked threads.

WAITING and BLOCKED states both prevent thread from running, but they are very different.

WAITING threads must be explicitly transformed to BLOCKED threads by a notify from some other thread.

WAITING never goes directly to RUNNABLE.

When RUNNABLE thread releases the lock (by leaving monitor or by waiting) one of BLOCKED threads automatically takes its place.

So to summarize, thread acquires the lock when it enters synchronized method or when it reenters the synchronized method after the wait.

public synchronized guardedJoy() {     // must get lock before entering here     while(!joy) {         try {             wait(); // releases lock here             // must regain the lock to reentering here         } catch (InterruptedException e) {}     }     System.out.println("Joy and efficiency have been achieved!"); } 
like image 66
cohadar Avatar answered Sep 25 '22 05:09

cohadar