Concurrent Collections
The Need
- Most of the traditional Collections are not threadsafe
- Vector, HashTable (All has performace related issues due to primitive synchronized method (takes a lock)) a. SunchronizedList b. SynchronizedSet c. SynchronizedMap
ConcurrentModification Exception (while one thread operating on a collection, other thread trying to modify, throws this exception)
Fail Fast and Fail Safe
Iterator returned by few Collection framework Classes are fail-fast structural modification -> during iteration throw ConcurrentModificationException.
Some important classes whose returned iterator is fail-fast > • ArrayList • LinkedList • vector • HashSet
While, Iterator returned by few Collection framework Classes are fail-safe, structural modification -> during iteration won’t throw any Exception. Some important classes whose returned iterator is fail-safe > • CopyOnWriteArrayList • CopyOnWriteArraySet • ConcurrentSkipListSet