Difference between Synchronized ArrayList and CopyOnWriteArrayList in Java

Java 8Object Oriented ProgrammingProgramming

Synchronized ArrayList and CopyOnWriteArrayList are useful for synchronizing the ArrayList. This is necessary for a multi-threaded environment to make sure thread safety is achieved.

The differences between Synchronized ArrayList and CopyOnWriteArrayList are given as follows −

Synchronized ArrayListCopyOnWriteArrayList
Synchronized ArrayList is used to synchronize the ArrayList.CopyOnWriteArrayList is used to synchronize the ArrayList.
The Java 1.2 version first introduced the Synchronized ArrayList.The Java 1.5 version first introduced the CopyOnWriteArrayList.
The Synchronized ArrayList should be used when there are more write operations than reading operations in ArrayList.The CopyOnWriteArrayList should be used when there are more read operations than write operations in ArrayList.
This iterator is a fail-fast iterator.This iterator is a fail-safe iterator.
The synchronized block should contain the iteration of the list.The iteration of the list can be outside the synchronized block.
During the read or write operation, the whole ArrayList is locked by Synchronized ArrayList for thread safety.During the write operation only, the whole ArrayList is locked by CopyOnWriteArrayList for thread safety.
The Synchronized ArrayList should be used when the ArrayList is larger.The CopyOnWriteArrayList should be used when the ArrayList is smaller.
raja
Updated on 30-Jul-2019 22:30:25

Advertisements