Java 5 – Introduction to Concurrent Collection

In this article, we will discuss Concurrent Collection in detail, covering

  • Why it is needed when already established Collection framework is in place from Java 1.2 version
  • What are the problems with Collection classes like ArrayList or HashSet or HashMap
  • What are the solutions provided by concurrent collection

Q) Why we need Concurrent Collection or What are the problems with Collection framework ?

Not thread-safe:

  • Collection classes like ArrayList or HashSet are used to represent/store a group of object/elements as a single unit/entity
  • But these collection classes aren’t thread-safe
  • So, while working with these collection classes in multi-threaded environment there is quite a possibility of data-consistency

Thread-safe version:

  • We can convert List or Set or Map implementation classes like ArrayList or HashSet or HashMap into thread-safe version using Collections class’ utility method
    Collections.synchronizedList(arrayList);
    b. Collections.synchronizedSet(hashSet);
    c. Collections.synchronizedMap(hashMap);
  • And there are few legacy collection classes like Vector or Hashtable introduced in Java 1.0 version
  • But problem with these collection classes is that, at any given point of time only one thread can operate after acquiring lock and rest of the threads need to wait for their turn
  • So, waiting time increases and hence affecting overall performance of the application
  • In short, performance is very low while working with these thread-safe collection classes

ConcurrentModificationException:

  • In a multi-threaded environment, while one thread is iterating over collection items then if any other threads tries to modify collection items, a runtime exception is thrown
  • That’s ConcurrentModificationException is thrown

Q) How to overcome above mentioned limitation in Collection framework ?

To overcome above mentioned problems, Sun (now Oracle) group came up with idea of Concurrent Collection where

  • New concurrent collection classes are always thread-safe
  • High performance comparing with Collection framework
  • And never throws ConcurrentModificationException

1. Advantages of Concurrent Collection:

  • Concurrent collection are always thread-safe
  • Performance is relatively high, when comparing with collection framework as it uses different locking strategy
  • While one thread iterating over collection items, other threads are allowed to do modification
  • Because there are different concurrency level or different locking strategy is used
  • Therefore, newly introduced concurrent collection classes NEVER throws ConcurrentModificationException
  • Concurrent collection is introduced in Java 1.5 version, to work with highly scalable multi-threaded environment
  • Example: ConcurrentHashMap, ConcurrentSkipListMap, CopyOnWriteArrayList, CopyOnWriteArraySet, ConcurrentSkipListSet, ArrayBlockingQueue, LinkedBlockingQueue
  • All concurrent collection classes present in java.util.concurrent package

2. Thread-safe Collection framework v/s New Concurrent Collection:

  • Thread-safe collection classes like Vector or Hashtable and converted thread-safe version of ArrayList, HashSet or HashMap obtain lock over complete collection items
  • Therefore, other threads need to wait for their turn to obtain lock before operating over collection items
  • Whereas, concurrent collection classes like ConcurrentHashMap or CopyOnWriteArrayList acquires lock over portion of List or Map objects
  • This is the key difference between them

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 5 - ConcurrentMap interface