Synchronization of ArrayList in Java

4.3

Implementation of arrayList is not synchronized is by default. It means if a thread modifies it structurally and multiple threads access it concurrently, it must be synchronized externally. Structural modification means addition or deletion of element(s) from the list or explicitly resizes the backing array. Changing the value of existing element is not structural modification.

There are two way to create Synchronized Arraylist.
1. Collections.synchronizedList() method.
2. Using CopyOnWriteArrayList.

Method 1: Using Collections.synchronizedList() method

To do serial access, it is critical that all access to the backing list is accomplished through returned list. It is imperative that user manually synchronize on the returned list when iterating over it.

public static  List<T> synchronizedList(List<T> list)
Accepts a List which could be implementation of List 
interface. e.g. ArrayList, LinkedList.
Returns a Synchronized(thread-safe) list backed by the 
specified list.
Parameter list is the list to be wrapped in a synchronize list.
T represents generic
// Java program to demonstrate working of
// Collections.synchronizedList
import java.util.*;

class GFG
{
    public static void main (String[] args)
    {
        List<String> list =
           Collections.synchronizedList(new ArrayList<String>());

        list.add("practice");
        list.add("code");
        list.add("quiz");

        synchronized(list)
        {
            // must be in synchronized block
            Iterator it = list.iterator();

            while (it.hasNext())
                System.out.println(it.next());
        }
    }
}

Output:

practice
code
quiz

Method 2: Using CopyOnWriteArrayList

  CopyOnWriteArrayList<T> threadSafeList = new CopyOnWriteArrayList<T>();
  
  Create an empty List.
  It implements List interface.
  It is a thread-safe variant of ArrayList.
  T represents generic

A thread-safe variant of ArrayList in which all mutative operations (e.g. add, set, remove..) are implemented by creating a separate copy of underlying array. It achieves thread-safety by creating a separate copy of List which is a is different way than vector or other collections use to provide thread-safety.

  • It is useful when you can’t or don’t want to synchronize the traversal, yet need to prevent interference among concurrent threads.
  • It is costly as involves separate Array copy with every write operation(e.g. add, set, remove..)
  • It is very efficient when you have List and need to traverse over its elements and don’t modify often it.

Iterator does not throw ConcurrentModificationException even if copyOnWriteArrayList is modified once iterator is created because iterator is iterating over the separate copy of ArrayList while write operation is happening on another copy of ArrayList.

// Java program to illustrate the thread-safe ArrayList.
import java.io.*;
import java.util.Iterator;
import java.util.concurrent.CopyOnWriteArrayList;

class GFG
{
    public static void main (String[] args)
    {
        // creating a thread-safe Arraylist.
        CopyOnWriteArrayList<String> threadSafeList
            = new CopyOnWriteArrayList<String>();

        // Adding elements to synchronized ArrayList
        threadSafeList.add("geek");
        threadSafeList.add("code");
        threadSafeList.add("practice");

        System.out.println("Elements of synchronized ArrayList :");

        // Iterating on the synchronized ArrayList using iterator.
        Iterator<String> it = threadSafeList.iterator();

        while (it.hasNext())
            System.out.println(it.next());
    }
}

Output:

Elements of synchronized ArrayList :
geek
code
practice

What happens if we try to modify CopyOnWriteArrayList through iterator’s own method?
It throws UnsupportedOperationException if you try to modify CopyOnWriteArrayList through iterator’s own method(e.g. add(), set(), remove()).

// Java program to illustrate the thread-safe ArrayList
import java.io.*;
import java.util.Iterator;
import java.util.concurrent.CopyOnWriteArrayList;

class GFG
{
    public static void main (String[] args)
    {
        // creating a thread-safe Arraylist.
        CopyOnWriteArrayList<String> threadSafeList =
            new CopyOnWriteArrayList<String>();

        // Adding elements to synchronized ArrayList
        threadSafeList.add("geek");
        threadSafeList.add("code");
        threadSafeList.add("practice");

        System.out.println("Elements of synchronized ArrayList :");

        // Iterating on the synchronized ArrayList using iterator.
        Iterator<String> it = threadSafeList.iterator();

        while (it.hasNext())
        {
            String str = it.next();
            it.remove();
        }
    }
}

Runtime Error:

Exception in thread "main" java.lang.UnsupportedOperationException
    at java.util.concurrent.CopyOnWriteArrayList$COWIterator.remove
        (CopyOnWriteArrayList.java:1176)
    at GFG.main(File.java:28)

Other constructors of CopyOnWriteArrayList :
1. CopyOnWriteArrayList(Collection<? extends E> c) : Creates a list containing the elements of the specified collection, in the order they are returned by the collection’s iterator.
2. CopyOnWriteArrayList(E[] toCopyIn) : Creates a list holding a copy of the given array.


Why to use arrayList when vector is synchronized?

  1. Performance: Vector is synchronized and thread-safe and because of this, it is slightly slower than ArrayList.
  2. Functionality: Vector synchronizes at the level of each individual operation. Generally a programmer like to synchronize a whole sequence of operations. Synchronizing individual operations is both less safe and slower.
  3. Vectors obsolete: Vectors are considered obsolete an d unofficially deprecated in java. Also,vector synchronizes on each individual operation which is almost never done. Mostly java programmers prefer using ArrayList since they will probably synchronize the arrayList explicitly anyway if they need to do synchronization.

Related Article: Vector vs ArrayList in Java

References:

This article is contributed by Nitsdheerendra. 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:



4.3 Average Difficulty : 4.3/5.0
Based on 3 vote(s)