CopyOnWriteArrayList concurrent collection in java (example)
Yogesh
CopyOnWriteArrayList is thread-safe variant of ArrayList.
New copy of underlying array is created when any add, set (or modification) operations performed on CopyOnWriteArrayList.
CopyOnWriteArrayList can be visualize as combination of words, COPY + On Write + ArrayList.
New copy of underlying array is created, when any write/update/remove operation is performed on CopyOnWriteArrayList.
i.e. When any kind of update happen on CopyOnWriteArrayList, new copy will be created.
CopyOnWriteArrayList has a fail-safe iterator ie. CopyOnWriteArrayList does not throw ConcurrentModificationException if underlying CopyOnWriteArrayList structurally modified during iteration.
1. When should we use CopyOnWriteArrayList?
CopyOnWriteArrayList can be used when there are few modifications and multiple threads perform read operations.
Copy of array is costly operations, we need to be very selective when choosing CopyOnWriteArrayList collection.
For example:
CopyOnWriteArrayList can be Publisher subscriber pattern.
Publisher will have registered subscribers during construction.
Publisher can be used in concurrent environment.
Publisher can iterate all subscribers.
Publisher can send notifications to all subscribers.
Its rarely the new subscriber will be registered with publisher (at latter stage).
2. Class hierarchy of CopyOnWriteArrayList:
3. Program: CopyOnWriteArrayList collection in java (example)
package org.learn;
import java.util.Arrays;
import java.util.List;
import java.util.ListIterator;
import java.util.concurrent.CopyOnWriteArrayList;
public class DemoCopyOnWriteArrayList {
public static void main(String[] args) throws InterruptedException {
final List<String> listWorldCupTeams = new CopyOnWriteArrayList<>
(
Arrays.asList("Brazil", "Germany", "Spain")
);
//Take snapshot of Original ArrayList
ListIterator<String> originalListIterator = listWorldCupTeams.listIterator();
System.out.println("1. Original CopyOnWriteArrayList is : " + listWorldCupTeams);
listWorldCupTeams.add("Argentina");
listWorldCupTeams.add("Netherlands");
System.out.println("2. Added teams Argentina & Netherlands to CopyOnWriteArrayList");
System.out.printf("3. Original CopyOnWriteArrayList is : [ ");
while (originalListIterator.hasNext()) {
System.out.printf(originalListIterator.next());
System.out.printf(" ");
}
System.out.printf("]");
ListIterator<String> iteratorPointingToNewList = listWorldCupTeams.listIterator();
System.out.printf("\n4. CopyOnWriteArrayList after adding elements : [ ");
while (iteratorPointingToNewList.hasNext()) {
System.out.printf(iteratorPointingToNewList.next());
System.out.printf(" ");
}
System.out.printf("]");
}
}
4. Output: CopyOnWriteArrayList collection in java (example)
1. Original CopyOnWriteArrayList is : [Brazil, Germany, Spain]
2. Added teams Argentina & Netherlands to CopyOnWriteArrayList
3. Original CopyOnWriteArrayList is : [ Brazil Germany Spain ]
4. CopyOnWriteArrayList after adding elements : [ Brazil Germany Spain Argentina Netherlands ]