Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As per the javadoc for <a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Object.html#wait%28%29" rel="nofollow">Object#wait()</a></p> <blockquote> <p>The <strong>current thread must own this object's monitor</strong>. 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.</p> </blockquote> <p>To use wait/notify the thread must have the lock, otherwise <a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/IllegalMonitorStateException.html" rel="nofollow">IllegalMonitorStateException</a> is thrown</p> <blockquote> <p>Throws: <strong>IllegalMonitorStateException</strong> - if the current thread is not the owner of the object's monitor.</p> </blockquote> <h2>Why</h2> <p>So, <code>wait()</code> makes current thread <em>release</em> the lock</p> <p><code>notify()</code> signals other waiting thread(s) which then <em>try to acquire</em> the lock.</p> <p>For doing either, the current thread <strong>must</strong> have the lock. And it makes sense!</p> <h2><em>Edit</em></h2> <ul> <li><p>Why must thread calling wait() hold the lock is pretty obvious now.</p></li> <li><p>But why must thread calling notify() hold the lock? Well, for one, to prove its <code>authenticity</code>. Otherwise any thread could keep firing <code>false notifications</code> and the <code>waiting</code> threads would keep getting interrupted. <em>Thankfully that is not the case.</em></p></li> </ul>
    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.
    3. 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