CopyOnWriteArrayList in Java

What is CopyOnWriteArrayList

CopyOnWriteArrayList is a concurrent Collection class introduced in Java 5 Concurrent API. It implements List interface like ArrayList, Vector and LinkedList but it is a thread-safe collection and it achieves its thread-safety in a slightly different way than Vector or other thread-safe collection class.

CopyOnWriteArrayList creates a fresh copy of the underlying array to implement all mutative operations like add, set etc. Creating a fresh copy is generally too expensive but may be more efficient than alternatives when traversal operations, i.e., iterations, vastly outnumber mutations, and is useful when you cannot or don’t want to synchronize traversals while accessing the List multiple concurrent threads.

Iterator of CopyOnWriteArrayList is fail-safe and guaranteed not to throw ConcurrentModificationException.

Element changing operations (add, remove, set) are not supported. So these operations will throw  UnsupportedOperationException.

CopyOnWriteArrayList vs ArrayList

  • CopyOnWriteArrayList is thread-safe and can be used in multi-threaded environment, whereas ArrayList is not thread-safe and cannot be used in multi-threaded environment.
  • Iterator of CopyOnWriteArrayList is fail-safe and does not throw ConcurrentModificationException but iterator of ArrayList is fail-fast and throws ConcurrentModificationException if modification is detected after iteration begins on ArrayList.
  • Remove operation on CopyOnWriteArrayList is not supported while remove operation on ArrayList is supported.

When should you use?

You should only use this Collection to read when you are doing upwards of more than 90% reads because updating this Collection a lot will kill performance. If you try to sort a CopyOnWriteArrayList you will see the list throws an UsupportedOperationException (the sort operation invokes set on the collection N times).

Example

package com.roytuts.collections;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
public class CopyOnWriteArrayListExample {
    public static void main(String[] args) {
        List<String> list = new CopyOnWriteArrayList<>();
        list.add("Java");
        list.add("XML");
        list.add("REST");
        list.add("SOAP");
        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()) {
            String string = (String) iterator.next();
            System.out.println(string);
        }
    }
}

Output

Java
XML
REST
SOAP

Change the while loop as shown below, you will get the UnsupportedOperationException, because remove operation on CopyOnWriteArrayList is not supported.

while (iterator.hasNext()) {
    String string = (String) iterator.next();
    System.out.println(string);
    iterator.remove();
}

Output

Java
Exception in thread "main" java.lang.UnsupportedOperationException
    at java.util.concurrent.CopyOnWriteArrayList$COWIterator.remove(CopyOnWriteArrayList.java:1176)
    at com.roytuts.collections.CopyOnWriteArrayListExample.main(CopyOnWriteArrayListExample.java:20)

Thanks for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *