Friday, January 29, 2016

Difference between ConcurrentHashMap and HashTable

Difference between ConcurrentHashMap and HashTable


The major advantage of using ConcurrentHashMap is “performance” as the lock is not applied on whole Map as is the case with Hashtable.
ConcurrentHashMap is offering all the features of Hashtable with a performance almost as good as a HashMap. ConcurrentHashMap’s accomplish this by a very simple mechanism. Instead of a map wide lock, the collection maintains a list of 16 locks by default, each of which is used to guard (or lock on) a single bucket of the map. This effectively means that 16 threads can modify the collection at a single time (as long as they’re all working on different buckets). Infact there is no operation performed by this collection that locks the entire map. The concurrency level of the collection, the number of threads that can modify it at the same time without blocking, can be increased. However a higher number means more overhead of maintaining this list of locks.

No comments:

Post a Comment