How to Synchronize an ArrayList in Java

By Preeti Jain Last updated : February 05, 2024

Problem statement

Given an ArrayList, write a Java program to get synchronized ArrayList.

Synchronizing an ArrayList in Java

Below are the two approaches for synchronizing an ArrayList in Java:

  1. Using synchronizedList() method
  2. Using CopyOnWriteArrayList<T> method

Synchronize an ArrayList in Java Using synchronizedList() Method

  • The synchronizedList() method is available in java.util package.
  • With the help of synchronizedList() method, we can make ArrayList synchronized.
  • This is a static method, it is accessible with the class name too. (i.e. If we try to access with the class object, in that case, we will not get any error or exception).
  • This method does not throw any exception at the time of synchronizing an ArrayList.

Syntax:

    public static List synchronizedList(List list);

Parameter(s):

  • list – represents the ArrayList to be binded in a synchronized list.

Return value:

The return type of this method is List, it returns the synchronized view of the given list.

Example to Synchronize an ArrayList in Java Using synchronizedList() Method

// Java program to demonstrate the example of 
// synchronizing an ArrayList by using synchronizedList() method

import java.util.*;

public class SynchronizeArrayList {
    public static void main(String[] args) {
        // ArrayList Declaration
        ArrayList al = new ArrayList();

        // By using add() method to add few elements in 
        //ArrayList
        al.add(10);
        al.add(20);
        al.add(30);
        al.add(40);
        al.add(50);

        // Display ArrayList 
        System.out.print("Display ArrayList : " + " ");
        System.out.println(al);

        Collections.synchronizedList(al);

        synchronized(al) {
            Iterator itr = al.iterator();
            System.out.println("Display synchronized ArrayList:");
            while (itr.hasNext())
                System.out.println(itr.next() + " ");
        }
    }
}

The output of the above example is:

Display ArrayList :  [10, 20, 30, 40, 50]
Display synchronized ArrayList:
10 
20 
30 
40 
50

Synchronize an ArrayList in Java Using CopyOnWriteArrayList<T> Method

  • The CopyOnWriteArrayList<T> is a synchronized thread-safe class.
  • In the case of CopyOnWriteArrayList, more than one threads are allowed to work on.
  • It works on different cloned copy for update operations.
  • During one thread iterating CopyOnWriteArrayList object and at the same time other thread can modify because it works on the separate cloned copy.

Example to Synchronize an ArrayList in Java Using CopyOnWriteArrayList<T> Method

// Java program to demonstrate the example of 
// synchronizing an ArrayList by using CopyOnWriteArrayList

import java.util.*;
import java.util.concurrent.*;

public class SynchronizeArrayList {
    public static void main(String[] args) {
        // CopyOnWriteArrayList Declaration
        CopyOnWriteArrayList < Integer > cowal = new CopyOnWriteArrayList < Integer > ();

        // By using add() method to add few elements in 
        // CopyOnWriteArrayList
        cowal.add(10);
        cowal.add(20);
        cowal.add(30);
        cowal.add(40);
        cowal.add(50);

        // Display ArrayList 
        System.out.print("Display CopyOnWriteArrayList : " + " ");
        System.out.println(cowal);

        // Iterate ArrayList using iterator()
        Iterator < Integer > itr = cowal.iterator();

        System.out.println("Display synchronized ArrayList:");
        while (itr.hasNext())
            System.out.println(itr.next() + " ");
    }
}

The output of the above example is:

Display CopyOnWriteArrayList :  [10, 20, 30, 40, 50]
Display synchronized ArrayList:
10 
20 
30 
40 
50 

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.