ConcurrentHashMap Vs HashTable
ConcurrentHashMap locking only applied for updates, only lock at segments level instead of the whole map.
HashTable locks the whole map.
- AssumeĀ
Hashtable
Ā andĀConcurrentHashMap
Ā are two types of Homes.Hashtable
Ā locks home's main door.ConcurrentHashMap
Ā locks specific room door instead of main door.
ConcurrentHashMap how it works
Concurrent hashmap by default has 16 segment of lock
- When setting the data in a particular segment, only the lock for that segment is obtained
- When getting data, no lock is used since it's volatile
However ConcurrentHashMap doesn't allow null
key and value