CopyOnWriteArrayList in java

CopyOnWriteArrayList: CopyOnWriteArrayList class is introduced in JDK 1.5, which implements List interface. It is enhanced version of ArrayList in which all modifications (add, set, remove, etc) are implemented by making a fresh copy.

Here are few points about CopyOnWriteArrayList:

  • As the name indicates, CopyOnWriteArrayList creates a Cloned copy of underlying ArrayList, for every update operation at certain point both will synchronized automatically which is takes care by JVM. Therefore there is no effect for threads which are performing read operation.
  • It is costly to use because for every update operation a cloned copy will be created. Hence CopyOnWriteArrayList is the best choice if our frequent operation is read operation.
  • It extends object and implements Serializable, Cloneable, Iterable<E>, Collection<E>, List<E> and RandomAccess
  • The underlined data structure is grow-able array.
  • It is thread-safe version of ArrayList.
  • Insertion is preserved, duplicates are allowed and heterogeneous Objects are allowed.
  • The main important point about CopyOnWriteArrayList is Iterator of CopyOnWriteArrayList can not perform remove operation otherwise we get Run-time exception saying UnsupportedOperationException.

Constructors Summary:

  • CopyOnWriteArrayList c = new CopyOnWriteArrayList(); :Creates an empty list.
  • CopyOnWriteArrayList c = new CopyOnWriteArrayList(Collection obj);:Creates a list containing the elements of the specified collection, in the order they are returned by the collection’s iterator.
  • CopyOnWriteArrayList c = new CopyOnWriteArrayList(Object[] obj);:Creates a list holding a copy of the given array.

Methods Summary:

  • public boolean addIfAbsent(Object obj)
  • public int addAllAbsent(Collection C)
// Java program to illustrate 
// CopyOnWriteArrayList class
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.*;

class ConcurrentDemo extends Thread {

    static CopyOnWriteArrayList l = new CopyOnWriteArrayList();

    public void run()
    {
        // Child thread trying to
        // add new element in the
        // Collection object
        l.add("D");
    }

    public static void main(String[] args) 
                throws InterruptedException
    {
        l.add("A");
        l.add("B");
        l.add("c");

        // We create a child thread 
        // that is going to modify 
        // ArrayList l.
        ConcurrentDemo t = new ConcurrentDemo();
        t.start();
        
        Thread.sleep(1000);
        
        // Now we iterate through 
        // the ArrayList and get
        // exception.
        Iterator itr = l.iterator();
        while (itr.hasNext()) {
            String s = (String)itr.next();
            System.out.println(s);
            Thread.sleep(1000);
        }
        System.out.println(l);
    }
}

Output:

A
B
c
D
[A, B, c, D]

Hello everyone, I am Bishal KUMAR Dubey and most importantly an idea creator. I just love Programming languages and love to know new concepts everyday,every minute,every second. Here to help Other GEEKS!!!

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:



0 Average Difficulty : 0/5.0
No votes yet.