HashMap Concurrent HashMap
ConcurrentHashMap ,SynchronisedHashMap and HashMap
This how use of collection HashMap ,Synchronized HashMap and ConcurrentHashMap. HashMap can be convert in to SynchronisedHashMap pretty similar to Hashtable . When there is concurrency we prefer to use ConcurrentHashMap this because its threadsafe without synchronization.
Concurrent HashMap::+++++++
public ConcurrentHashMap (int initialCapacity, float loadFactor, int concurrencyLevel)
initialCapacity - the initial capacity. The implementation performs internal sizing to accommodate this many elements.concurrencyLevel - the estimated number of concurrently updating threads. The implementation performs internal sizing to try to accommodate this many threads.
In the ConcurrentHashMap Api , you will find the following constants.
static final int DEFAULT_INITIAL_CAPACITY = 16;
static final int DEFAULT_CONCURRENCY_LEVEL = 16;
initial capacity parameter and concurrency level parameters of ConcurrentHashMap constructor (or Object) are set to 16 by default.
In the ConcurrentHashMap Api , you will find the following constants.
static final int DEFAULT_INITIAL_CAPACITY = 16;
static final int DEFAULT_CONCURRENCY_LEVEL = 16;
initial capacity parameter and concurrency level parameters of ConcurrentHashMap constructor (or Object) are set to 16 by default.
Important facts about the ConcurrentHashMap
- It is thread safe without synchronizing the whole map.
- Reads can happen very fast while write is done with a lock.
- There is no locking at the object level.
- The locking is at a much finer granularity at a hashmap bucket level.
- ConcurrentHashMap doesn’t throw a ConcurrentModificationException if one thread tries to modify it while another is iterating over it.
- ConcurrentHashMap uses multitude of locks.
- Faster then SynchronisedHashMap . This because it does not require object level lock.
- Does not allow null key .
SynchronisedHashMap::++++++
- Synchronization at Object level.
- Every read/write operation needs to acquire lock.
- Locking the entire collection is a performance overhead.
- This essentially gives access to only one thread to the entire map & blocks all the other threads.
- It may cause contention.
- SynchronizedHashMap returns Iterator, which fails-fast on concurrent modification.
HashMap :::++++++
- It is not synchronised .
- Faster performance then Concurrent and synchronised HashMap.
- Not thread safe .
- Allow one null key and multi null value .
Why ConcurrentHashMap does not allow null keys and null values ?
The main reason that nulls aren't allowed in ConcurrentMaps (ConcurrentHashMaps, ConcurrentSkipListMaps) is that ambiguities that may be just barely tolerable in non-concurrent maps can't be accommodated. The main one is that if map.get(key) returns null, you can't detect whether the key explicitly maps to null vs the key isn't mapped. In a non-concurrent map, you can check this via map.contains(key), but in a concurrent one, the map might have changed between calls.
The main reason that nulls aren't allowed in ConcurrentMaps (ConcurrentHashMaps, ConcurrentSkipListMaps) is that ambiguities that may be just barely tolerable in non-concurrent maps can't be accommodated. The main one is that if map.get(key) returns null, you can't detect whether the key explicitly maps to null vs the key isn't mapped. In a non-concurrent map, you can check this via map.contains(key), but in a concurrent one, the map might have changed between calls.
HashMap Concurrent HashMap
Reviewed by Mukesh Jha
on
3:09 AM
Rating:
No comments:
Add your comment