Concurrent Collection
Need of concurrent collection because of the following issues in traditional collections — 1) Most of the collections like ArrayList…
Concurrent Collection
Need of concurrent collection because of the following issues in traditional collections —
-
Most of the collections like ArrayList, LinkedList, HashSet etc are not thread safe which may result in data inconsistency problem. Concurrent collection is thread safe.
-
Some few collections like Vector, HashTable, synchronizedList are thread safe but they are not perfromance efficient as they lock total collection object hence only 1 thread is allowed to operate on the entire object at a time. Not even reading opeartion can be performed by multiple threads at the same time. Concurrent collection has high perfromance because of the different locking mechanism is used.
-
If one thread is iterating a collection object then other thread cannot modify it. If it tries to modify the object then ConcurrentModificationException will be thrown. Concurrent collection never throws ConcurrentModificationException. If one thread is iterating a collection object then remaining threads can modify the object in safe manner.
Concurrent collections are best for scalable multi threaded operation.

concurrent collection classes.
ConcurrentHashMap —

ConcurrentHashMap
ConcurrentHashMap contains all methods of Map interface. ConcurrentMap contains 3 more methods as shown.
Difference between put(key, value) in Map and putIfAbsent(key, value) in ConcurrentMap is that put() replaces the existing key in the map while putIfAbsent() only adds a key only if the key is not already present in the Map else it ignores the operation. Difference between remove(key) in Map and remove(key, value) in ConcurrentMap is that remove(key) removes the existing key in the map while remove(key, value) only removes a key only if both the key and value are matched else it ignores the operation. replace(key, oldValue, newValue) — if both key and value are matched then only replace the old value with new value.



methods in ConcurrentHashMap
HashMap vs HashTable vs ConcurrentHashMap —
Any number of threads can perform any operation on HashMap object hence it is not thead safe. Only 1 thread can operate on HashTable object hence it is thead safe but it increases waiting time of remaining thread thereby creating performance problem. ConcurrentHashMap object is also thread safe but it uses different locking mechanism thereby solving performance issue.
Locking mechanism used by ConcurrentHashMap —
No lock is required to perform read operation i.e multiple threads can perform read operation simultaneously. Lock is required only for write/update operation. Also locking is done segement wise. By default bucket size(size of map) is 16 and default number of locks are also 16 i.e 1 lock for each bucket. If update/write operation is performed at bucket number 1 then the remaining threads can perform update operation in remaining buckets. Hence 16 threads can perform update operation simultaneously. Number of locks(by default 16) is known as concurrency level. Notice that instead of object level lock it is using segment level lock thereby improving performance.

Consider that number of buckets is 16 and concurrency level is 8. It means that a lock will be applied on 2 buckets i.e only 1 thread can perform updation in 2 buckets. If one thread is performing updation in bucket 15 then no other thread will be able to perform update operation either on 15 or 14.

ConcurrentHashMap points
Difference between HashMap and ConcurrentHashMap —

Difference between HashMap and ConcurrentHashMap
Difference between ConcurrentHashMap, synchronizedMap and Hashtable —

Difference between ConcurrentHashMap, synchronizedMap and Hashtable
CopyOnWriteArrayList —
It is thread safe version of ArrayList. Multiple threads can perform the read operation on its object. If any thread performs a write operation, a separate cloned copy of the object will be created and update will be performed on it thereby not affecting the original object on which any other thread can perform read. The original object and the newly created object will be synced by JVM. Note that if a large number of update operations are performed then a large number of copies will be created resulting in performance issue hence it should be used only when less write operations are to be performed.

CopyOnWriteArrayList
Difference between ArrayList and CopyOnWriteArrayList —

Difference between ArrayList and CopyOnWriteArrayList

Note that iterator of ArrayList can perform remove opearation but iterator of CopyOnWriteArrayList cannot perform remove opearation.
Methods of CopyOnWriteArrayList —
It contains all methods provided by Collection and List interface. It has few additional methods —
- addIfAbsent() — adds an element only if it is already not present in the list.
- addAllAbsent() — adds all element of a collection which are already not present.


methods of CopyOnWriteArrayList
Difference between ArrayList and CopyOnWriteArrayList —

Difference between ArrayList and CopyOnWriteArrayList
Difference between CopyOnWriteArrayList,synchronizedList and Vector —

Difference between CopyOnWriteArrayList,synchronizedList and Vector
CopyOnWriteArraySet —
It is thread safe version of ArraySet. Multiple threads can perform the read operation on its object. It is implemented using CopyOnWriteArrayList. If any thread performs a write operation, a separate cloned copy of the object will be created and update will be performed on it thereby not affecting the original object on which any other thread can perform read. The original object and the newly created object will be synced by JVM. Note that if a large number of update operations are performed then a large number of copies will be created resulting in performance issue hence it should be used only when less write operations are to be performed.

Methods of CopyOnWriteArraySet —
It contains all methods provided by Collection and Set interface. It has no additional methods.
Difference between CopyOnWriteArraySet,synchronizedSet and Vector —

Difference between CopyOnWriteArraySet,synchronizedSet and Vector
Fail safe vs Fail fast Iterator —
If one iterator is iterating a collection object,other iterator performing any write/update fails raising ConcurrentModificationException then such type of iterators are called fail fast iterators. If one iterator is iterating a collection object,other iterator performing any write/update does not fail and do not raise ConcurrentModificationException then such type of iterators are called fail safe iterators.
메타데이터
- post_id
- 65b8280da7b5
- slug
- concurrent-collection-65b8280da7b5
- url
- https://medium.com/@prakherjindal1996/concurrent-collection-65b8280da7b5
- canonical_url
- https://medium.com/@prakherjindal1996/concurrent-collection-65b8280da7b5
- author_url
- https://medium.com/@prakherjindal1996
- status
- ok
- fetched_at
- 2026-08-01 14:51:42