14

It is an advocated paradigm that wait() should be invoked inside a while loop inside a synchronized block.

My question is how does the waiting() thread get the lock back ?

// Thread 1
    synchronized (mon) {
     while (!condition) 
          mon.wait();

    // Do something
    }

//Thread 2
    synchronized (mon) {//set condition appropriately
            mon.notify();
    }

Consider the thread 1 runs first and starts waiting for the condition. It releases the lock and the thread 2 obtains the lock sets the condition and notifies thread 1. Now thread 1 gets the lock, checks the condition and starts executing "do something".

My question is when Thread 1 is notified it starts execution from the while condition, the line of code which had Synchronized(mon) is never executed again then how does thread 1 acquire the lock ? What are the internal dynamics that give the lock back to Thread 1 ?

4 Answers 4

9

When Thread1 is notified the thread has to acquire the lock before it can exit the wait method, see the java doc for Object#wait:

The thread T is then removed from the wait set for this object and re-enabled for thread scheduling. It then competes in the usual manner with other threads for the right to synchronize on the object; once it has gained control of the object, all its synchronization claims on the object are restored to the status quo ante - that is, to the situation as of the time that the wait method was invoked. Thread T then returns from the invocation of the wait method. Thus, on return from the wait method, the synchronization state of the object and of thread T is exactly as it was when the wait method was invoked.

4
  • @Nathan : I think we know the answer :-) We don't(at least I don't understand) this part of the Java Doc : Thus, on return from the * {@code wait} method, the synchronization state of the object and of * thread {@code T} is exactly as it was when the {@code wait} method * was invoked.
    – Geek
    May 26, 2013 at 15:16
  • @Geek: what don't you understand? exiting from the wait method puts the thread in a synchronized block, so it needs to acquire the lock first. otherwise it would be executing code in a synchronized block without having acquired the lock. May 26, 2013 at 15:21
  • 1
    @NathanHughes : I understand it starts from the same state as it was when the wait() call was invoked. Does it mean that the lock is released from inside of the wait() function, the wait() function doesn't execute till the last line of the code in the function, then re-acquires the lock after being notified and then the wait() finishes and code starts to run from application code ?
    – Geek
    May 26, 2013 at 15:35
  • @Geek: yes, wait finishes once the object waited on has the lock again. May 26, 2013 at 15:41
7

synchronized(mon) is not an expression that has to be executed.

It's a syntax element in the source code that tells the compiler (and then the runtime) that the wrapped section of the code must only be executed after the lock associated with mon has been acquired by the current thread, even if you don't "come from" the line of code before the synchronized block.

wait() releases the lock, and must reacquire it before returning.

5
  • Your answer is absolutely correct. Can you let me know how this metadata information works at run time. Can you please point me to some resources where I can read about it.
    – Geek
    May 26, 2013 at 15:11
  • I guess you could find what you're looking for in the JVM specifications, but I have never read them.
    – JB Nizet
    May 26, 2013 at 15:13
  • 1
    Calling wait notifies the JVM that thread acquiring the lock wants to relinquish it. Its JVM which puts the thread in wait queue and releases the lock. When notify(all) is called then the signal is sent to the JVM that thread which acquired the lock (whose notify(all) has been called) has released the lock and the any eligible candidate (which are there in wait queue put by JVM) is the selected by JVM to acquire the lock. Look at this link to understand the control flow booxs.biz/images/java/thread-states.png
    – Saurabh
    May 26, 2013 at 15:22
  • 3
    @saury: to be exact, the notifying thread doesn't release the lock when notify() is called. It's released only when it exits from its synchronized block.
    – JB Nizet
    May 26, 2013 at 15:26
  • @JBNizet Critical piece of the puzzle. Thx for explaining.
    – Julian A.
    Jan 25, 2016 at 8:25
0

After Thread 1 is notified, it got the lock immediately and start to run //Do something.

When Thread 1 wait, it just release the lock temporarily, and when the thread is notified, it can get the lock again and needn't run synchronized(...).

0
// Thread 1
 synchronized (mon) {
 Systemout.println("I am invoked!");
 while (!condition) 
      mon.wait();

// Do something
}

//Thread 2
synchronized (mon) {//set condition appropriately
        mon.notify();
}

In the original scenario: Consider the thread 1 runs first and starts waiting for the condition. It releases the lock and the thread 2 obtains the lock sets the condition and notifies thread 1. Now thread 1 gets the lock, checks the condition and starts executing "do something".

If my understand the following correctly:

The thread T is then removed from the wait set for this object and re-enabled for thread scheduling. It then competes in the usual manner with other threads for the right to synchronize on the object; once it has gained control of the object, all its synchronization claims on the object are restored to the status quo ante - that is, to the situation as of the time that the wait method was invoked. Thread T then returns from the invocation of the wait method. Thus, on return from the wait method, the synchronization state of the object and of thread T is exactly as it was when the wait method was invoked.

the line Systemout.println("I am invoked!"); will not be executed, as "Thus, on return from the wait method, the synchronization state of the object and of thread T is exactly as it was when the wait method was invoked."

Am I right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.