The thread-safe version of an ArrayList is CopyOnWriteArrayList. The following table enumerates the Java built-in single-threaded and multithreaded lists:
Single thread |
Multithreaded |
ArrayList LinkedList |
CopyOnWriteArrayList (often reads, seldom updates) Vector |
The CopyOnWriteArrayList implementation holds the elements in an array. Every time we invoke a method that mutates the list (for example, add(), set(), and remove()), Java will operate on a copy of this array.
An Iterator over this collection will operate on an immutable copy of the collection. Therefore, the original collection can be modified without issues. Potential modifications of the original collection are not visible in the Iterator:
List<Integer> list = new CopyOnWriteArrayList<>();
Use this collection when reads are frequent and changes are seldom.