Understanding CopyOnWriteArrayList in Java: Implementation, Principles, and Comparison with ArrayList
CopyOnWriteArrayList is a thread‑safe variant of ArrayList that achieves read‑write separation by copying the underlying array on each mutative operation, using a ReentrantLock for writes, making it ideal for read‑heavy, write‑light scenarios, and it differs from ArrayList in safety, performance, and concurrency behavior.
When people talk about read‑write separation they often think of MySQL master‑slave setups, but Java also provides a collection that follows the same principle: CopyOnWriteArrayList (and its sibling CopyOnWriteArraySet ).
1. Overview of CopyOnWriteArrayList
The class implements the List interface and is a thread‑safe variant of ArrayList . All mutative operations (add, set, remove, etc.) are performed by creating a fresh copy of the underlying array, as described in the official Javadoc:
A thread‑safe variant of ArrayList in which all mutative operations (add, set, and so on) are implemented by making a fresh copy of the underlying array.
2. Implementation principle
The core fields are a re‑entrant lock for write operations and a volatile array that holds the elements:
final transient ReentrantLock lock = new ReentrantLock();
private transient volatile Object[] array;When an element is added, the method locks, copies the current array, appends the new element, and then replaces the reference:
public boolean add(E e) {
final ReentrantLock lock = this.lock;
lock.lock();
try {
Object[] elements = getArray();
int len = elements.length;
Object[] newElements = Arrays.copyOf(elements, len + 1);
newElements[len] = e;
setArray(newElements);
return true;
} finally {
lock.unlock();
}
}This shows that every write acquires the lock, copies the whole array, modifies the copy, and then publishes the new array, while reads simply fetch the current array without any locking:
public E get(int index) {
return get(getArray(), index);
}Because writes are expensive (they copy the entire backing array), CopyOnWriteArrayList is best suited for scenarios with many reads and few writes, such as lookup tables for product categories or geographic data.
3. Comparison with ArrayList
The following table highlights the main differences:
CopyOnWriteArrayList
ArrayList
Thread‑safe
Thread‑unsafe
Higher efficiency for reads
Higher efficiency for writes
Iterating thread can see modifications without
ConcurrentModificationExceptionIterating thread throws
ConcurrentModificationExceptionif modified
Fast‑safe
Fast‑fail
Part of
java.util.concurrentpackage
Part of
java.utilpackage
Introduced in Java 1.5
Introduced in Java 1.2
References:
http://www.benchresources.net/copyonwritearraylist-vs-arraylist-in-java/
http://www.makeinjava.com/copyonwritearraylist-concurrent-collection-java-example/
Full-Stack Internet Architecture
Introducing full-stack Internet architecture technologies centered on Java
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.