public interface ConcurrentMap<K,V> extends Map<K,V>
A ConcurrentMap providing thread safety and atomicity guarantees. its extends Map interface.
public class ConcurrentHashMap<K,V> extends AbstractMap<K,V>
implements ConcurrentMap<K,V>, Serializable
Type Parameters:
K - the type of keys maintained by this map
V - the type of mapped values
All Implemented Interfaces:
Serializable, ConcurrentMap<K,V>
A ConcurrentHashMap providing thread safety and atomicity guarantees. It is a part of concurrent collections framework since java 1.5 and is present in java.util.concurrent package.
Concurrency-Level = No. of Multiple threads can access it at same time.
Note: Part of the map called Segment (internal data structure).
ConcurrentHashMap is fail-safe in nature i.e while one thread is Iterating the ConcurrentHashMap object and if other thread try to add/modify the contents of Object then it will not throw ConcurrentModificationException exception.
ConcurrentHashMap performance is slower than HashMap due to thread-safety because sometimes Threads are required to wait on ConcurrentHashMap.
DEFAULT_CONCURRENCY_LEVEL : 16 by default if not given any concurrency-Level.
DEFAULT_INITIAL_CAPACITY: 16 default
Load-Factor : 0.75 load factor threshold for resizing.
Read Operation at ConcurrentHashMap: At a time any number of threads are applicable for read operation without locking the ConcurrentHashMap object.
Write Operation at ConcurrentHashMap: This is depend on concurrency-level i.e number of segment you have passed into it. The Object is divided into number of segments according to the concurrency level. Default concurrency-level of ConcurrentHashMap is 16.
ConcurrentHashMap does not allow to store null key or null value. Any attempt to store null key or value throws runtimeException (NullPointerException).
How the ConcurrentHashMap maintain thread safety.
ConcurrentHashMap we have segments which basically extend ReentrantLock that means ConcurrentHashMap use Lock API.
How the ConcurrentHashMap work internally.
ConcurrentHashMap utilizes the same principles of HashMap.
As we know Hashtable and HashMap both uses Bucket (array and linkedlist) as the data structure to store the data.Bucket is list of array.
Now ConcurrentHashMap create one more array on top of it. called segments.
In Java 8, It is a tree structure instead of linked-list to further enhace the performance.
Constructor in ConcurrentHashMap:-
- ConcurrentHashMap(): This will creates a new empty map object, with a default initial capacity (16), load factor (0.75) and concurrencyLevel (16).
- ConcurrentHashMap(int initialCapacity): This will creates a new empty map object with the specified initial capacity, and with default load factor (0.75) and concurrencyLevel (16).
- ConcurrentHashMap(int initialCapacity, float loadFactor): This will creates a new empty map object with the specified initial capacity and load factor and with the default concurrencyLevel (16).
- ConcurrentHashMap(int initialCapacity, float loadFactor, int concurrencyLevel):This will creates a new empty map object with the specified initial capacity, load factor and concurrency level.
- ConcurrentHashMap(Map<? extends K,? extends V> m):This will creates a new empty map object with the same mappings as the given map.
Related Tutorials
- Lock Interface In Concurrency API
- Lock In Java Example
- StampedLock With Examples
- Differences between Lock and Synchronized block
- Method And Block Synchronization In Java Example
- How To Create Custom Lock
- Fair Lock In Java Example
- Synchronization Vs Lock in java
- Thread Pool - ThreadPoolExecutor Example
- How To Create Custom Thread Pool
- How ConcurrentHashMap Works Internally
- newsinglethreadexecutor vs newfixedthreadpool
- Future Vs Completablefuture
- ExecutorService vs ExecutorCompletionService in Java
- Callable Interface Example
- CompletableFuture
- ExecutorCompletionService
- Method And Block Synchronization
- ConcurrentHashMap
- Differences Between Submit and Execute methods
- Difference Between Callable and Runnable Interface in Java
No comments:
Post a Comment