Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have to <strong>disagree</strong> with people who say <strong><code>notifyAll()</code> releases the lock</strong> on the object over which waiting and notifying threads are being synchronized. </p> <p>An example:</p> <p><code>Consumer</code> class contains a block:</p> <pre><code>synchronized(sharedObject){ if(sharedObject.isReadyToConsume() == false){ sharedObject.wait(); }else { sharedObject.doTheThing(); System.out.println("consumer consuming..."); } </code></pre> <p>}</p> <p>Scenario: <strong>Consumer class</strong> gets the lock on the <strong>sharedObject</strong> object, enters exclusively (it's inside the sync block) and sees that <strong>sharedObject</strong> has nothing ready yet (nothing to consume :) ) and it calls <code>wait()</code> method on the <strong>sharedObject</strong>. That way it releases the lock (stops the execution there!) and waits to be notified to continue when another Thread (Producer maybe) calls <code>sharedObject.notify();</code> or <code>sharedObject.notifyAll();</code>. When it gets notified it continues from the <strong>wait()</strong> line </p> <p>It's the <strong>sharedObject</strong> that keeps track of threads that asked it to be notified. When some Thread calls <strong>sharedObject.notifyAll()</strong> method the <strong>sharedObject</strong> will notify the waiting threads to wake up... Now, the tricky part is that a thread naturally <strong>releases the lock of the object when it reaches the end of its synchronized(sharedObject){} block</strong>. THe question is <strong>what happens if I call notifyAll() in that block</strong>??? <strong>notifyAll()</strong> wakes up the waiting threads, but the lock is still owned by the Thread that has just call <strong>notifyAll()</strong></p> <p>Look at the Producer snippet:</p> <pre><code>synchronized(sharedObject){ //We are exlusively working with sharedObject and noone can enter it [... changing the object ...] sharedObject.notifyAll(); //notifying the waiting threads to wake up Thread.sleep(1000); //Telling the current thread to go to sleep. It's holding the LOCK System.out.println("awake..."); </code></pre> <p>}</p> <p>If <strong>notifyAll()</strong> would release the lock then the "awake..." would get printed out after the Consumer classes already start working with the <strong>sharedObject</strong>. This is not the case... The output shows that the Consumer is consuming the sharedObject after the Producer exits its sync block...</p> <ul> <li><em>wait()</em> - releases the lock and continues on the next line when it gets notified</li> <li><em>notify(), notifyAll()</em> - don't release the lock. They simply make waiting threads runnable again (not idle). They will have the right to enter when the current thread reaches the end of its sync block and the Thread scheduleder tells them that the lock has been released. The fight for the lock begins again</li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload