CopyOnWriteArrayList
CopyOnWriteArrayList is a thread-safe list that copies its array on every write. Iterators see a snapshot and never throw ConcurrentModificationException. Writes are expensive. Reads are cheap.
Detailed Explanation
Some lists are read almost always and written rarely: listener lists, subscriber registries, a small set of plugins. Locking on every read would hurt. CopyOnWriteArrayList stores elements in an immutable array. get and iteration read that array with no lock. add, set, and remove clone the array, apply the change, and publish the new array.
Iterators walk the array that existed when iterator() was called. Later writes do not appear. There is no fail-fast. remove on the iterator is unsupported. That snapshot is the feature: a listener can unsubscribe while you are firing events, and the current dispatch still sees the old list.
You pay a full array copy per write, plus garbage for the old array. A list of thousands of elements updated on every request is the wrong shape. A list of a dozen listeners updated on deploy is the right one.
You reach for it when iteration must be safe without locking and writes are rare. You reach for Collections.synchronizedList when writes are common and you can lock around a whole iteration. You reach for ConcurrentLinkedQueue when you only need a concurrent queue. You do not use copy-on-write as a general ArrayList replacement.
CopyOnWriteArraySet is a set version backed by the same idea. Both allow null. Equality and index methods scan the array (O(n)). Random get is O(1).
The cost of a hidden write in a loop is O(n²) copies. Add in batches if you must load many elements: construct from a collection in one constructor call. The cost of assuming an iterator sees your write is a stale-view bug; that is by design.
Code Examples
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
public class Listeners {
public static void main(String[] args) {
CopyOnWriteArrayList<String> listeners = new CopyOnWriteArrayList<>();
listeners.add("audit");
listeners.add("metrics");
for (String name : listeners) {
if (name.equals("audit")) {
listeners.remove("metrics"); // safe; iterator still sees the snapshot
}
}
System.out.println(listeners); // prints: [audit]
}
}import java.util.concurrent.CopyOnWriteArrayList;
public class Snapshot {
public static void main(String[] args) {
CopyOnWriteArrayList<Integer> xs = new CopyOnWriteArrayList<>(List.of(1, 2));
var it = xs.iterator();
xs.add(3);
System.out.println(it.next()); // prints: 1
System.out.println(it.next()); // prints: 2
System.out.println(it.hasNext()); // prints: false (snapshot was [1, 2])
System.out.println(xs); // prints: [1, 2, 3]
}
}Common Gotchas
Every `add` copies the whole array
do not use this as a hot write path.
Iterators are snapshots
they will not see writes that happen after `iterator()` and they reject `remove`.
`indexOf` is O(n)
this is not a concurrent `ArrayList` with free random writes.
Treating it as a drop-in `ArrayList` under load will burn CPU and GC
Treating it as a drop-in `ArrayList` under load will burn CPU and GC