Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm assuming that the class at the top is Q, it's missing some code. Anyway, the general idea is that the boolean <code>valueSet</code> and the <code>wait()</code>/<code>notify()</code> calls work in tandem. </p> <p>If the consumer has already begun to wait, he has acquired the lock to the Q instance through the synchronized <code>get()</code> method and then releases it while he waits.</p> <p>If the consumer has not begun to wait yet, the producer may have the lock to the Q instance because the <code>put()</code> method is synchronized on that same lock. Once the producer exits the lock, he will have called <code>notify()</code> <em>as well as set the valueSet boolean to true</em>.</p> <p>The consumer's next call to <code>get()</code> will read the boolean value <em>before attempting to wait</em>, notice that something's there, take the contents of <code>n</code> and do whatever work is needed. If the value had not been set, meaning that nothing had come in in the consumer's absence, he would <code>wait()</code> on the lock for new work and the next <code>notify()</code> will wake him up.</p> <p><strong>Update</strong></p> <p>In your scenario in the comments, you're essentially asking about the exact same situation but in reverse. The same logic applies. </p> <p>The consumer is currently waiting and the producer calls <code>notify()</code>. The producer currently has the lock and will continue to hold the lock for the duration of the method. All <code>notify()</code> does is let one other thread currently waiting on the lock know that, when the lock is released, it can try to take the lock and resume execution. In this case, there is only one other thread, but if there were multiple threads then it would select only one (to wake everyone, <code>notifyAll()</code> should be called).</p> <ol> <li>The producer exits the method, releasing the lock.</li> <li>The consumer wakes up and takes the lock.</li> </ol> <p>At this point, it's ambiguous as to whether the producer has already come around and is currently waiting on the lock or if it has not entered the method yet. The same tandem of the boolean flag and <code>wait()</code>/<code>notify()</code> applies to this as well.</p> <p>Before the consumer releases the lock by exiting the method, it will both set the boolean flag to false and call <code>notify()</code>.</p> <p>If the producer is currently already waiting on the lock, calling <code>notify()</code> will let it know that it can wake up and continue when the lock is released.</p> <p>If the producer is not waiting through a <code>wait()</code> call, it must be outside the method (possibly waiting to enter the method and acquire the lock that way). When the consumer exits the method and releases the lock, the producer acquires it and checks the boolean flag. It's been set to false, therefore the producer <em>does not attempt to call</em> <code>wait()</code> and just drops its value off, sets the boolean flag and calls <code>notify()</code>.</p>
    singulars
    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.
 

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