Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

How does CopyOnWriteArrayList work and when is it a good choice?

CopyOnWriteArrayList never changes its backing array in place: every add, set or remove copies the array under a lock, changes the copy, and publishes it with a volatile write. Reads and iteration use whatever array is current without any locking, so an iterator sees a fixed snapshot, never throws ConcurrentModificationException, and does not support remove, set or add. That makes it a good choice for small, read-mostly lists such as event listeners or configuration entries, and a poor one for large or frequently modified lists, because every write costs O(n) time and allocation.

Why this question is asked

Sooner or later every service has a list that many threads read and a few threads occasionally change: the listeners notified when an order ships, the interceptors in a pipeline, the list of downstream endpoints. Using an ArrayList there leads to ConcurrentModificationException or worse, and wrapping it with a lock makes every read wait. CopyOnWriteArrayList is the JDK's answer for this shape of problem. Interviewers ask about it because it is a clear example of trading write cost for read speed, and because people regularly use it where that trade is wrong. In this answer, we will recap the problem it solves, see how writes and reads work, look at what its iterators can and cannot do, and decide when to use it.

1A Quick Recap of the Problem

An ArrayList is not safe to share. If one thread iterates while another adds, the iterator notices that the list's modification count changed and throws ConcurrentModificationException, or in an unlucky interleaving reads a half-updated state. Collections.synchronizedList fixes the corruption but still requires us to hold its lock for the whole iteration, so a slow listener blocks every thread that wants to add or read.

CopyOnWriteArrayList, from java.util.concurrent, solves it differently. Instead of protecting one mutable array, it makes every array it publishes effectively immutable. Change is done by replacing the array, not by editing it.

2How a Write Works

Every mutating method follows the same three steps. It takes the list's internal lock, so writers never overlap. It copies the current array into a new one with the change applied: one element longer for add, one shorter for remove, one slot different for set. Then it stores the new array in a volatile field, which makes the complete new array visible to every thread that reads the field afterwards.

Java
List<String> names = new CopyOnWriteArrayList<>(List.of("a", "b", "c"));
names.add("d"); // lock, copy 3 elements into a new array of 4, write "d", publish, unlock
names.set(0, "z"); // lock, copy 4 elements, replace slot 0, publish, unlock
names.remove("b"); // lock, find "b", copy the other 3 into a new array, publish, unlock
System.out.println(names); // [z, c, d]
Step throughA writer and a reader at the same time
WriterList fieldReader
read array v1
lock, copy v1 to v2, add
iterate v1 unchanged
volatile write: field = v2
next read sees v2

5 messages in order. Play them, or step through them one at a time.

The reader keeps the array it started with; the writer never touches it.

So every write costs O(n) time and allocates a new array of n references. For a list of 10 listeners that is nothing. For a list of 50,000 elements, each write copies 50,000 references, roughly 200 KB of fresh garbage with compressed references, which is where the trouble with large lists comes from. Since Java 9 the lock is an internal monitor rather than a ReentrantLock, but the behaviour is the same.

3How Reads and Iteration Work

Reads take no lock at all. get(i) reads the volatile field once and indexes into that array. iterator() reads the field once and walks that array to the end. Because nobody ever modifies a published array, the iterator cannot see a half-done change, and it has no reason to throw ConcurrentModificationException.

Java
List<String> list = new CopyOnWriteArrayList<>(List.of("a", "b"));
Iterator<String> it = list.iterator(); // snapshot of [a, b]
list.add("c"); // creates a new array; the iterator keeps the old one
while (it.hasNext()) System.out.print(it.next() + " "); // a b
System.out.println(list); // [a, b, c]
Iterator<String> it2 = list.iterator();
it2.next();
it2.remove(); // UnsupportedOperationException

Notice that the iterator printed a b even though c was added before the loop ran. That is the snapshot guarantee: an iterator shows the list exactly as it was when the iterator was created, no more, no less. It is a stronger guarantee than the weakly consistent iterators of ConcurrentHashMap, which may or may not show later changes.

The flip side is that the iterator cannot change the list. Its remove, set and add methods throw UnsupportedOperationException, because changing a snapshot that other readers may also hold would break the whole design. To remove elements while scanning, we call list.removeIf(predicate), which performs the scan and the copy as one write under the lock.

4When It Is a Good Choice

The design pays off when reads vastly outnumber writes and the list is small. The textbook case is a listener list: listeners are registered at start-up and maybe removed at shutdown, but events are fired thousands of times per second, each firing iterating the whole list. With CopyOnWriteArrayList, firing an event is a lock-free loop over an array, and a listener that registers another listener during the loop does not break anything.

It is a poor choice in three situations. Large lists make every write expensive in time and garbage. Frequent writes, such as an item added per request, turn the copying into the main cost of the service. And code that needs to see writes immediately inside a long iteration will not, because it holds a snapshot. A useful rule of thumb is that if the list is changed more than a few times per second, or holds more than a few thousand elements, look at ConcurrentLinkedQueue, a concurrent set from ConcurrentHashMap.newKeySet(), or a different design.

Choosing a shared list
WorkloadGood fitWhy
Small, read on every event, rarely changedCopyOnWriteArrayListLock-free reads, snapshot iteration
Large, changed oftenConcurrentHashMap.newKeySet()O(1) add and remove, no copying
Producer and consumer order mattersConcurrentLinkedQueue or a BlockingQueueDesigned for constant churn
Read and write equally, smallsynchronizedList with careSimple, but lock iteration

5Follow-up questions

Is CopyOnWriteArrayList strongly consistent for a single get?

Each get sees the array that was current when it read the volatile field, which includes every write completed before that read. Two consecutive gets can see different arrays if a write happens in between, so code that needs several reads to agree should take one snapshot, for example by iterating once or by calling toArray.

What is CopyOnWriteArraySet?

It is a set built on a CopyOnWriteArrayList. add uses addIfAbsent, which scans the array, so contains and add are O(n). It has the same snapshot iterators and suits the same small, read-mostly cases, such as a set of listeners where duplicates must be avoided.

Does addAll help with the write cost?

Yes. addAll copies the array once for the whole batch instead of once per element, so adding 100 elements costs one copy instead of 100. When writes come in groups, batching them is the simplest way to reduce the copying.

6Conclusion

In this answer, we saw that CopyOnWriteArrayList handles every write by copying the backing array under a lock and publishing the new array with a volatile write, while reads and iteration use the current array without locking. Its iterators are true snapshots that never throw ConcurrentModificationException and do not support remove, set or add. That makes it ideal for small, read-mostly lists like listeners and configuration, and a poor fit for large or frequently modified lists, where each write's O(n) copy and allocation dominate.

What to say in the interviewAbout 80 seconds aloud

Open with

CopyOnWriteArrayList never modifies an array that readers might be using. Every write, add, set or remove, takes an internal lock, copies the whole backing array with the change applied, and then publishes the new array through a volatile field, so other threads see the complete new version.

Then, if they want more

Reads don't lock at all: get just indexes into the current array, and an iterator grabs the array once and walks it. Because that array never changes, the iterator is a true snapshot.

It never throws ConcurrentModificationException, it doesn't see writes made after it was created, and its remove, set and add throw UnsupportedOperationException; if I need to remove while scanning I use removeIf on the list. The cost is on the write side: every write is O(n) in time and allocates a full new array.

So it's a great fit for small, read-mostly lists, the classic example being event listeners, registered once and iterated on every event, or a short list of configuration entries or interceptors. It's a poor fit for large lists or lists that change frequently.

For example, with tens of thousands of elements changing many times a second, the copying becomes the dominant cost, and I would use something like a concurrent set from ConcurrentHashMap.newKeySet or a queue instead.

About this answer

Last updated
12 November 2026
Versions it names
Java 9
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "How does CopyOnWriteArrayList work and when is it a good choice?": https://scrivio-interview-library.netlify.app/java/concurrent-collections/89ca443c5d/