Difference Between Synchronized ArrayList and CopyOnWriteArrayList in Java Collection
As we know that the ArrayList is not synchronized, if multiple threads try to modify an ArrayList at the same time, then the final outcome will be non-deterministic. Hence synchronizing the ArrayList is a must to achieve thread safety in a multi-threaded environment.
In order to make List objects we were generally creating objects of the List interface and there making List classes as per our requirements and lately adding elements and were accessing, updating without having a thought about thread safety. This concept is easy and at the same time, a bit advanced because it is seen most Java developers do not practice this technique while writing codes.
Note: Synchronized ArrayList is synchronized collection while CopyOnWriteArrayList is an concurrent collection as it is made with keeping concurrency.
Different Ways of Achieving Synchronization in ArrayList
Synchronization in an Arraylist can be achieved in two ways:
- Using synchronizedList() Method of Collections Class
- Using CopyOnWriteArrayList (COWAL)
Since both ways are used to achieve thread-safety in Arraylist. The question appears, when to use COWAL and when to use synchronizedList() method of Collections class. This can be understood by understanding the differences between them. The main difference between synchronized ArrayList and CopyOnWriteArrayList comes from their performance, scalability, and how they achieve thread safety.
Why CopyOnWriteArrayList came into existence when Collection.synchronizedList() was already present?
So the answer is pretty simple because initially, SynchronizedList was used in a multithreaded environment but it had some limitations. All of its read and write methods were synchronized on the list object itself, i.e. if a thread is executing add() method, it blocks other threads which want to get the iterator to access elements in the list. Also, only one thread was allowed to iterate the list’s elements at a time, which was inefficient. That was quite rigid. Thus a more flexible collection was required which allows:
- Multiple threads executing read operations concurrently.
- One thread executes the read operation and another executes the write operation concurrently.
- Only one thread can execute write operations while other threads can execute read operations simultaneously.
To overcome these issues, finally, in Java 5, a new set of collection classes called Concurrent Collections was introduced which had CopyOnWriteArrayList in it. The CopyOnWriteArrayList class is designed to enable such sequential write and concurrent reads features.
Let us discuss characteristics associated with both of them that create a thin line of difference between them that are as listed below:
1. Locking of Threads
Synchronized List locks the whole list to provide synchronization and thread safety during the read or write operation, while, CopyOnWriteArrayList doesn’t lock the whole list during these operations.
The CopyOnWriteArrayList class works according to its name i.e. copy-on-write which performs different actions for reading and write operations. For every write operation (add, set, remove, etc), it makes a new copy of the elements in the list. and for the read operations (get, iterator, listIterator, etc), it works on a different copy. So there is no additional overhead during a read operation and its read operation is faster than Collections.SynchronizedList(). Thus, COWAL is better for reading operation than Synchronized List.
2. Write Operations
For write operation in ArrayList, COWAL write operations are slower than Collections.synchronizedList(), since it uses Re-entrantLock. The write method will always create a copy of the existing array and do the modification on the copy and then finally update the volatile reference of the array to point to this new array. Therefore, it has massive overhead during a write operation. That’s why CopyOnWriteArrayList write operations are slower than Collections.synchronizedList().
3. Behavior During Modification
Synchronized List is a fail-fast iterator, i.e. it will throw ConcurrentModifcationException when the list is modified when one thread is iterating over it whereas CopyOnWriteArrayList is a fail-safe iterator, i.e. it will not throw ConcurrentModifcationException even when the list is modified when one thread is iterating over it.
4. Number of Threads Working
Only one thread is allowed to operate on Synchronized List, by locking over the complete list object which affects its performance since other threads are waiting whereas, in the case of COWAL, multiple threads are allowed to operate on ArrayList, as it works on separate cloned copy for update/modify operations which makes its performance faster.
5. Iterating within Block
While iterating synchronized List, make sure to iterate inside the synchronized block whereas, in CopyOnWriteArrayList, we can safely iterate outside the synchronized block.
When to use SynchronizedList?
- Since in CopyOnWriteArrayList for every update/modify operation, a new separate cloned copy is created and there is overhead on JVM to allocate memory and merge cloned copy with the original copy. Thus, in this case, SynchronizedList is a better option.When the size of Arraylist is large.
- When size of Arraylist is large.
When to use CopyOnWriteArrayList?
- The CopyOnWriteArrayList provides reading without a lock, which means a much better performance if there are more reader threads and writing is happening quite low.
- When the size of Arraylist is small.
SynchronizedList v/s CopyOnWriteArrayList
|It was introduced in Java version 1.2||It was introduced in Java version 1.5|
|It should be used when there are more write operations over-read operations.||It should be used when there are more read operations than write operations.|
|The iterator used is fail-fast.||The iterator used is fail-safe.|
|The iteration of List has to be there inside the synchronized block.||The iteration of the list can be outside the synchronized block.|
|The whole ArrayList is locked by Synchronized Arraylsit for thread safety during read and write operations.||The whole ArrayList is locked by SynchronizedArrayList for thread safety during the write operations only.|
|It is preferred when ArrayList is larger.||It is preferred when ArrayList is smaller.|