In this article, we will discuss difference between CopyOnWriteArrayList and SynchronizedList classes in detail i.e.; CopyOnWriteArrayList v/s SynchronizedList
Lets us move on and discuss key differences between these 2 List classes
1. SynchronizedList v/s CopyOnWriteArrayList:
CopyOnWriteArrayList | SynchronizedList |
CopyOnWriteArrayList is newly introduced thread-safe class (i.e.; synchronized) | This is thread-safe version of List i.e.; any List implemented classes like ArrayList or LinkedList can be converted into synchronized List (thread-safe) |
Multiple threads are allowed to operate on CopyOnWriteArrayList, as it works on separate cloned copy for update/modify operations | Only one thread is allowed to operate on synchronized list, by locking over complete list object |
While one thread iterating CopyOnWriteArrayList object, other threads happily can modify, as it works on separate cloned copy
And it never throws ConcurrentModificationException | While one thread iterating List object, if any other threads tries to modify the same List object then ConcurrentModificationException is thrown |
That’s it is fail-safe iterator | That’s it is fail-fast iterator |
There is no such restriction while iterating CopyOnWriteArrayList;
We can safely iterate outside synchronized block | While iterating synchronized List, make sure to iterate inside synchronized block;
Otherwise we may face non-deterministic behavior |
Iterator of CopyOnWriteArrayList can perform read operation safely; while iterating through COWAL items
But as soon as, remove operation is performed, compiler throws UnsupportedOperationException | Iterator of List can perform both read and remove operations; while iterating through List elements |
This is introduced in Java 1.5 version | This is introduced in original collection framework in Java 1.2 version |
Q) When to use SynchronizedList ?
- This is generally used to convert list object into thread-safe list object
- But only one thread is allowed to operate on list object, as lock is acquired over complete list object
- So, performance degrades comparatively in a multi-threaded environment
- So, use this only when it is required to convert into thread-safe version of List object
- Otherwise, mature and latest CopyOnWriteArrayList can be used efficiently
Q) When to use CopyOnWriteArrayList ?
- This is the best suit to store elements as per insertion order in a multi-threaded environment
- Where there are more number of read operation and very less update/modify operation
- Because for every update/modify operations, a new separate cloned copy is created
- And there is overhead on JVM to allocate memory & merging cloned copy with original copy
- The advantage of using CopyOnWriteArrayList over ArrayList is that, it doesn’t throws ConcurrentModificationException when multiple threads perform operation simultaneously
2. CopyOnWriteArrayList v/s SynchronizedList :
- there is always a catch between performance and thread-safety
- choose wisely for your requirement
Related Articles:
- Java 5 – Introduction to Concurrent Collection
- Java 5 – CopyOnWriteArrayList class with example
- Java 5 – CopyOnWriteArrayList with Read and Update operations simultaneously
- Java 5 – Remove operation with CopyOnWriteArrayList and ArrayList
- Java 5 – ArrayList v/s CopyOnWriteArrayList
- Java 5 – CopyOnWriteArrayList v/s SynchronizedList
- Java 5 – Concurrent Collection Interview question and answers
References:
- https://docs.oracle.com/javase/tutorial/collections/intro/
- https://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Map.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Map.Entry.html
- https://docs.oracle.com/javase/tutorial/collections/interfaces/map.html
- https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html
- https://docs.oracle.com/javase/tutorial/essential/concurrency/collections.html
- https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ConcurrentMap.html
- https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ConcurrentHashMap.html
Happy Coding !!
Happy Learning !!