title: ArrayBlockingQueue and add vs put vs capacity
body: <p>From Javadoc of <code>ArrayBlockingQueue</code> <a href="http://download.oracle.com/javase/6/docs/api/java/util/concurrent/ArrayBlockingQueue.html#add%28E%29">ArrayBlockingQueue</a>:  </p>
  
  <blockquote>
    <p>add</p>
    
    <p>public boolean add(E e)</p>
  
  <pre><code>Inserts the specified element at the tail of this queue if it is possible 
  to do so immediately without exceeding the queue's capacity, returning true 
  upon success and throwing an IllegalStateException if this queue is full.
  </code></pre>
  </blockquote>
  
  <p>I always interpretted this statement (the part <code>if it is possible to do so immediattely</code>) as  follows:  </p>
  
  <blockquote>
    <p>If the queue has free capacity, then the insert will succeed. If there is no empty space then it will not succeed.  </p>
  </blockquote>
  
  <p>But my understanding was wrong here.  </p>
  
  <p>In a simple case that I decided to use an <code>ArrayBlockingQueue</code> for e.g. 20 elements (small queue) and having one thread doing:  </p>
  
  <p><code>queue.take()</code></p>
  
  <p>the other thread did not add an element to the queue via the <code>add</code> method despite the queue was almost empty.  </p>
  
  <p>I verified it also via debugging.  </p>
  
  <p>Once I replaced the call of <code>queue.add(element)</code> to <code>queue.put(element)</code> the element was indeed added to the queue.  </p>
  
  <p>So what is so different in these to methods?  </p>
  
  <p>For what other reason (besides capacity) could the addition not happen?</p>
  
  <hr>
  
  <p><strong>UPDATE:</strong>  </p>
  
  <pre><code>public class ConnectionListener implements Observer {
  
    public static BlockingQueue&lt;ConnectionObject&gt; queueConnections = new   ArrayBlockingQueue&lt;ConnectionObject&gt;(10);
  
    @Override
    public void update(Observable arg0, Object arg1) {
        ConnectionObject con = ((ConnectionObject)arg1);
        queueConnections.add(con);
    }
  
  }  
  </code></pre>
  
  <p><code>ConnectionObject</code> is just a holder for String values.  </p>
  
  <pre><code>public class ConnectionObject {
    private String user;  
    private String ip;
     //etc  
  }
  </code></pre>
  
  <p>And the consumer:  </p>
  
  <pre><code>public class ConnectionTreeUpdater extends Thread {  
    @Override
    public void run() {
      while(true){  
      try {  
      final ConnectionObject con = ConnectionListener.queueConnections.take();
  </code></pre>
  
  <p>If I use <code>add</code> no exception is thrown but element does not get added to the queue.  </p>
  
  <p>Just a thought: perhaps since the consumer is "waiting" on the queue, if for some internal housekeeping the element can not be added it will not be added and no exception is thrown.Could that be the case.  </p>
  
  <p>Otherwise I can not understand why there is no exception and with <code>put</code> the code works.  </p>
  
  <p>Are <code>put</code> and <code>add</code> meant to be used differently?  </p>