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. See Lock Stripping