Backend Development 7 min read

Copy‑On‑Write Strategy and Its Implementation in Java's CopyOnWriteArrayList

The article explains the copy‑on‑write (COW) concurrency strategy, its suitability for read‑heavy scenarios, and demonstrates how Java implements COW with CopyOnWriteArrayList and CopyOnWriteArraySet, including volatile array handling, write‑locking, snapshot reads, and iterator behavior.

Cognitive Technology Team
Cognitive Technology Team
Cognitive Technology Team
Copy‑On‑Write Strategy and Its Implementation in Java's CopyOnWriteArrayList

Copy‑On‑Write (COW) is a concurrency technique where, instead of modifying the original data directly, a copy of the data is created, the modification is performed on the copy, and finally the reference is switched to the new copy, leaving the original data untouched.

This approach incurs extra memory overhead due to the copy operation, making it most appropriate for scenarios with many reads and few writes, such as whitelist/blacklist management.

In concurrent designs, write operations are protected by a lock while read operations proceed without locking; reads rely on the visibility guarantees of the array reference, providing eventual consistency but not real‑time consistency.

Java provides COW implementations through CopyOnWriteArrayList and CopyOnWriteArraySet . The underlying container holds a volatile Object[] array for visibility and a private lock object for exclusive write access.

Key fields in CopyOnWriteArrayList :

final transient Object lock = new Object();
/** The array, accessed only via getArray/setArray. */
private transient volatile Object[] array;

Write operations acquire the lock, copy the current array, apply the modification, and then replace the original reference:

public boolean add(E e) {
    synchronized (lock) {
        Object[] es = getArray();
        int len = es.length;
        es = Arrays.copyOf(es, len + 1);
        es[len] = e;
        setArray(es);
        return true;
    }
}

Reading does not lock; it uses the volatile array for memory visibility. For example, the indexOf method works on a snapshot of the array:

public int indexOf(E e, int index) {
    Object[] es = getArray();
    return indexOfRange(e, es, index, es.length);
}

final Object[] getArray() {
    return array;
}

Before iteration, a snapshot of the current array is taken, ensuring that the iterator works on a stable view while the underlying list may be modified concurrently.

The iterator class COWIterator stores the snapshot and cursor, and all mutating methods (remove, set, add) throw UnsupportedOperationException to prevent modifications of the snapshot:

static final class COWIterator
implements ListIterator
{
    /** Snapshot of the array */
    private final Object[] snapshot;
    private int cursor;
    COWIterator(Object[] es, int initialCursor) {
        cursor = initialCursor;
        snapshot = es;
    }
    public boolean hasNext() { return cursor < snapshot.length; }
    public E next() { if (!hasNext()) throw new NoSuchElementException(); return (E) snapshot[cursor++]; }
    public void remove() { throw new UnsupportedOperationException(); }
    // other mutating methods also throw UnsupportedOperationException
}

In summary, the essential points of Java's COW strategy are:

The array reference must be declared volatile to guarantee visibility.

All write operations copy the entire array, modify the copy, and then replace the original reference under a lock.

Read operations obtain a temporary snapshot of the array and proceed without locking, providing eventual consistency.

Iterators operate on a snapshot and do not support modification.

COW is highly efficient for read‑dominant workloads (e.g., whitelists, blacklists, Docker image handling) but incurs additional memory consumption.

JavaBackend DevelopmentconcurrencyData Structurescopy-on-writecopyonwritearraylist
Cognitive Technology Team
Written by

Cognitive Technology Team

Cognitive Technology Team regularly delivers the latest IT news, original content, programming tutorials and experience sharing, with daily perks awaiting you.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.