Java 5 – CopyOnWriteArraySet v/s SynchronizedSet

In this article, we will discuss difference between CopyOnWriteArraySet and SynchronizedSet classes in detail i.e.; CopyOnWriteArraySet v/s SynchronizedSet

Lets us move on and discuss key differences between these 2 Set classes

1. CopyOnWriteArrayList v/s SynchronizedSet :

  • COWAL –> CopyOnWriteArrayList
  • COWAS –> CopyOnWriteArraySet
CopyOnWriteArraySetSynchronizedSet
CopyOnWriteArrayList is newly introduced thread-safe class (i.e.; synchronized)This is thread-safe version of Set i.e.; any Set implemented classes like HashSet or TreeSet can be converted into synchronized Set (thread-safe)
Multiple threads are allowed to operate on CopyOnWriteArraySet, as it works on separate cloned copy for update/modify operationsOnly one thread is allowed to operate on synchronized set, by locking over complete list object
While one thread iterating CopyOnWriteArraySet object, other threads happily can modify, as it works on separate cloned copy

 

And it never throws ConcurrentModificationException

While one thread iterating synchronized set object, if any other threads tries to modify the same Set 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 on CopyOnWriteArraySet;

 

We can safely iterate outside synchronized block

While iterating synchronized Set, make sure to iterate inside synchronized block;

 

Otherwise we may face non-deterministic behavior

Iterator of CopyOnWriteArraySet can perform read operation safely; while iterating through COWAS items

 

But as soon as, remove operation is performed, compiler throws UnsupportedOperationException

Iterator of Set can perform both read and remove operations; while iterating through Set elements
This is introduced in Java 1.5 versionThis is introduced in original collection framework in Java 1.2 version

Q) When to use SynchronizedSet ?

  • This is generally used to convert set object into thread-safe set object
  • But only one thread is allowed to operate on set object, as lock is acquired over complete set 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 Set object
  • Otherwise, mature and latest CopyOnWriteArraySet can be used efficiently

Q) When to use CopyOnWriteArraySet ?

  • This is the best suit to store unique 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 memorymerging cloned copy with original copy
  • The advantage of using CopyOnWriteArraySet over Set is that, it doesn’t throws ConcurrentModificationException when multiple threads perform operation simultaneously

2. CopyOnWriteArraySet v/s SynchronizedSet :

  • 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 v/s HashSet