Java 5- CopyOnWriteArrayList v/s SynchronizedList

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:

CopyOnWriteArrayListSynchronizedList
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 operationsOnly 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 iteratorThat’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 versionThis 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:

References:

Happy Coding !!
Happy Learning !!

Java 5 - CopyOnWriteArraySet class with example
Java 5 - CopyOnWriteArrayList v/s ArrayList