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
HashtableandConcurrentHashMapare two types of Homes.Hashtablelocks home's main door.ConcurrentHashMaplocks 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. See Lock Stripping