CopyOnWriteArrayList concurrent collection in java (example)

  1. CopyOnWriteArrayList is thread-safe variant of ArrayList.
  2. New copy of underlying array is created when any add, set (or modification) operations performed on CopyOnWriteArrayList.
  3. 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.
  4. 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:
    1. CopyOnWriteArrayList can be Publisher subscriber pattern.
    2. Publisher will have registered subscribers during construction.
    3. Publisher can be used in concurrent environment.
      • Publisher can iterate all subscribers.
      • Publisher can send notifications to all subscribers.
    4. Its rarely the new subscriber will be registered with publisher (at latter stage).

2. Class hierarchy of CopyOnWriteArrayList:

copyonwritearraylist class hierarchy java
Fig 1: CopyOnWriteArrayList class hierarchy

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 ]
Scroll to Top