Home »
Java programming language
How to synchronize ArrayList in Java?
Synchronizing ArrayList: Here, we are going to learn how to synchronize an ArrayList in Java programming language?
Submitted by Preeti Jain, on September 13, 2019
Synchronizing ArrayList
In java, there are two ways to synchronize ArrayList,
- With the help of synchronizedList() method
- With the help of CopyOnWriteArrayList<T> method
1) Synchronizing ArrayList using synchronizedList(List list) method
- This method is available in java.util package.
- With the help of this 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:
// 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() + " ");
}
}
}
Output
Display ArrayList : [10, 20, 30, 40, 50]
Display synchronized ArrayList:
10
20
30
40
50
2) Synchronizing ArrayList using CopyOnWriteArrayList
- CopyOnWriteArrayList 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:
// 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() + " ");
}
}
Output
Display CopyOnWriteArrayList : [10, 20, 30, 40, 50]
Display synchronized ArrayList:
10
20
30
40
50
TOP Interview Coding Problems/Challenges