title: Java Synchronization - Mutex.wait vs List.wait
body: <p>While using Java Threading Primitives to construct a thread safe bounded queue - whats the difference between these 2 constructs</p>
  
  <ol>
  <li>Creating an explicit lock object.</li>
  <li>Using the list as the lock and waiting on it.</li>
  </ol>
  
  <p><strong>Example of 1</strong></p>
  
  <pre><code>private final Object lock = new Object();
  private ArrayList&lt;String&gt; list = new ArrayList&lt;String&gt;();
  
  public String dequeue() {
      synchronized (lock) {
          while (list.size() == 0) {
              lock.wait();
          }
  
          String value = list.remove(0);
          lock.notifyAll();
          return value;
      }
  }    
  
  public void enqueue(String value) {
      synchronized (lock) {
          while (list.size() == maxSize) {
              lock.wait();
          }
  
          list.add(value);
          lock.notifyAll();
      }
  }
  </code></pre>
  
  <p><strong>Example of 2</strong></p>
  
  <pre><code>private ArrayList&lt;String&gt; list = new ArrayList&lt;String&gt;();
  
  public String dequeue() {
      synchronized (list) {  // lock on list
          while (list.size() == 0) {
             list.wait();     // wait on list
          }
  
          String value = list.remove(0);
          list.notifyAll();  
          return value;
      }
  }
  
  
  public void enqueue(String value) {
      synchronized (list) {  // lock on list
          while (list.size() == maxSize) {
             list.wait();   // wait on list
          }
  
          list.add(value);
          list.notifyAll();
      }
  }
  </code></pre>
  
  <p>Note</p>
  
  <ol>
  <li>This is a bounded list </li>
  <li>No other operation is being performed apart from enqueue and dequeue. </li>
  <li>I could use a blocking queue, but this question is more for improving my limited knowledge of threading.</li>
  <li>If this question is repeated please let me know.</li>
  </ol>