title: Difference between CopyOnWriteArrayList and synchronizedList
body: <p>As per my understanding concurrent collection classes preferred over synchronized collections because the concurrent collection classes don't take a lock on the complete collection object. Instead they take locks on a small segment of the collection object.</p>
  
  <p>But when I checked the <code>add</code> method of <code>CopyOnWriteArrayList</code>, we are acquiring a lock on complete collection object. Then how come <code>CopyOnWriteArrayList</code> is better than a list returned by <code>Collections.synchronizedList</code>? The only difference I see in the <code>add</code> method of <code>CopyOnWriteArrayList</code> is that we are creating copy of that array each time the <code>add</code> method is called.</p>
  
  <pre><code>public boolean add(E e) {
      final ReentrantLock lock = this.lock;
      lock.lock();
      try {
          Object[] elements = getArray();
          int len = elements.length;
          Object[] newElements = Arrays.copyOf(elements, len + 1);
          newElements[len] = e;
          setArray(newElements);
          return true;
      } finally {
          lock.unlock();
      }
  }
  </code></pre>