How do java.util.concurrent.locks.Condition work?
<p>Reading the Java 8 documentation about the <code>java.util.concurrent.locks.Condition</code> interface, the following example is given:</p>

<pre class="lang-java prettyprint-override"><code>class BoundedBuffer {
   final Lock lock = new ReentrantLock();
   final Condition notFull  = lock.newCondition(); 
   final Condition notEmpty = lock.newCondition(); 

   final Object[] items = new Object[100];
   int putptr, takeptr, count;

   public void put(Object x) throws InterruptedException {
     lock.lock();
     try {
       while (count == items.length)
         notFull.await();
       items[putptr] = x;
       if (++putptr == items.length) putptr = 0;
       ++count;
       notEmpty.signal();
     } finally {
       lock.unlock();
     }
   }

   public Object take() throws InterruptedException {
     lock.lock();
     try {
       while (count == 0)
         notEmpty.await();
       Object x = items[takeptr];
       if (++takeptr == items.length) takeptr = 0;
       --count;
       notFull.signal();
       return x;
     } finally {
       lock.unlock();
     }
   }
 }
</code></pre>

<p>So my main question is: How does a condition work?</p>

<ol>
<li>Does it release the lock when it starts waiting? (<code>notFull.await()</code> for example)?</li>
<li>Can different threads acquire the same lock and move until it <code>signal()</code> a condition, awaking other threads?</li>
<li>I thought this example would result in a deadlock because if a thread is waiting for the buffer to <em>not be empty</em> and it hasn't release the lock yet, how can another thread acquire the lock, empty the buffer, <code>signal()</code> the condition is now met, and release the lock <em>if the lock hasn't been release by the thread waiting for the buffer to be not full</em>?</li>
</ol>

<p>These are beginners questions. Please help me out.</p>

<p>Thank you.</p>