Monday 25 November 2013

Difference between Hashtable and ConcurrentHashMap and HashMap

Difference between Hashtable and ConcurrentHashMap and HashMap


HashMap
HashTable
ConcurrentHashMap
Not Synchronized
Synchronized
ConcurrentHashMap and CopyOnWriteArrayList implementations provide much higher concurrency while preserving thread safety, with some minor compromises in their promises to caller
Hashmap allow one null key and Multiple null value
Not allow null keys or null values
ConcurrentHashMap do not allow null keys or null values
COLLECTION CLASS
LEGACY CLASSS
COLLECTION CLASS
Works in only Single thread models access
Once the size of hashtable becomes considerable large performance degrade because for iteration it has to be locked for longer duration.
Since ConcurrentHashMap introduced concept of segmentation , how large it becomes only certain part of it get locked to provide thread safety so many other readers can still access map without waiting for iteration to complete.
Not  Loked
Locked on Whole  Map
In Summary ConcurrentHashMap only locked certain portion of Map
You can make HashMap synchronized by wrapping it on Collections.synchornizedMap(HashMap) which will return a collection which is almost equivalent to Hashtable, where every modification operation on Map is locked on Map object

while in case of ConcurrentHashMap, thread-safety is achieved by dividing whole Map into different partition based upon Concurrency level and only locking particular portion instead of locking whole Map. by default  size is 16
HashMap only slightly better. in Single thread Environment
bad performance
ConcurrentHashMap is more scalable and performs better than Synchronized HashMap in multi-threaded environment
Collections.synchronizedMap and Collections.synchronizedList, provide a basic conditionally thread-safe implementation of Map and List. However, several factors make them unsuitable for use in highly concurrent applications  for example their single collection-wide lock is an impediment to scalability and it often becomes necessary to lock a collection for a considerable time during iteration to prevent ConcurrentModificationException.


Does not maintain Insertion order
it maintain Insertion order





Reference :-


No comments:

Post a Comment

Very Impotent Links for Java and Portal development development

Jar Download for Spring  Maven Or Gradel Dependency for Spring