Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Collections

The CopyOnWriteArrayList class

Reference for java.util.concurrent.CopyOnWriteArrayList: how snapshot iterators work, what every write costs, and when it beats a synchronized list.

CopyOnWriteArrayList<E> is a thread-safe List in java.util.concurrent. Every mutating operation (add, set, remove, clear, ...) works on a fresh copy of the internal array and then publishes it. Readers never lock, and an iterator keeps looking at the array that existed when it was created.

That design makes reads and iteration very cheap and completely safe against concurrent modification, at the price of an O(n) copy for each write. It fits lists that are read constantly and changed rarely, such as listener registries or configuration lists. A sibling class, CopyOnWriteArraySet, gives the same behavior for sets.

Java
List<String> listeners = new CopyOnWriteArrayList<>();
listeners.add("audit");

Try it

Java Snapshot iterators

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Iterators cannot modify the list

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
public class CopyOnWriteArrayList<E>
    implements List<E>, RandomAccess, Cloneable, Serializable
Member Description
CopyOnWriteArrayList() Creates an empty list.
CopyOnWriteArrayList(Collection<? extends E> c) Creates a list holding the elements of c in iteration order.
CopyOnWriteArrayList(E[] array) Creates a list from a copy of the array.
boolean addIfAbsent(E e) Adds e only if it is not already present; returns whether it was added. Atomic.
int addAllAbsent(Collection<? extends E> c) Adds the elements that are not yet present; returns how many were added.
boolean removeIf(Predicate<? super E> filter) Removes matching elements with a single copy.
Iterator<E> iterator() Snapshot iterator; remove(), set() and add() throw UnsupportedOperationException.

Usage notes

  • Cost: reads by index are O(1); every write copies the whole array, O(n). Ten thousand single add calls on a big list are very slow, so batch changes with addAll or removeIf.
  • Snapshot semantics: an iterator never throws ConcurrentModificationException and never shows later changes. It may therefore show elements that were already removed from the list.
  • Visibility: a write happens-before later reads, so other threads see a consistent list without extra synchronization. A compound action ("check, then add") still needs addIfAbsent or an external lock.
  • Memory: a snapshot kept by a slow iterator keeps the old array alive. Many writers plus many iterators can create heavy garbage.
  • Compared with Collections.synchronizedList: the synchronized wrapper locks every call and still needs manual locking while iterating. The copy-on-write list does not lock readers at all.
  • Not for write-heavy queues. Use ConcurrentLinkedQueue, LinkedBlockingQueue or a ConcurrentHashMap based structure instead.
  • Nulls are allowed.