title: How does 2 thread able to acquire lock after notifyAll?
body: <p>I was reading about inter thread communication in Java using wait/notify/notifyAll.</p>
  
  <p>I went thru this answer: <a href="https://stackoverflow.com/a/36276832">https://stackoverflow.com/a/36276832</a></p>
  
  <p>There are 2 threads and 1 main thread.  Main thread do <code>notifyAll</code>. It wakes up remaining  2 threads, and both the threads prints </p>
  
  <pre><code>":syncronized block have finished"
  </code></pre>
  
  <p>But i have read that if 2 threads are waiting for a lock, notifyAll will wake up each thread but lock will be acquired by only 1 thread.</p>
  
  <p>So my question is how come both t1 and t2 thread are completing their execution?</p>
  
  <p>When I change from <code>lock.notifyAll();</code> to <code>lock.notify();</code>, the Java program never ends.</p>
  
  <p>One thread either of t1/t2 will be in waiting state.</p>
  
  <p>Could somebody please answer. I can explain further my doubt in case not understood clearly.</p>
  
  <p><strong>Problem in easy words:</strong> If 2 threads are waiting for same lock and 3rd thread do notifyAll, only one of them gets the lock, the other one remain in waiting state, so in the above case, how come both threads are able to complete execution?</p>