Thursday, June 3, 2010

Difference between ConcurrentHashMap and Collections.synchronizedMap

1. ConcurrentHashMap - It allows concurrent modification of the Map from several threads without the need to block them.
Collections.synchronizedMap(map) creates a blocking Map which will degrade performance, albeit ensure consistency (if used properly).

2. Use the second option if you need to ensure data consistency, and each thread needs to have an up-to-date view of the map. Use the first if performance is critical, and each thread only inserts data to the map, with reads happening less frequently.

3. ConcurrentHashMap is optimized for concurrent access.

4. Hashtable and ConcurrentHashMap do not allow null keys or null values.

Collections.synchronizedMap(Map) synchronizes all operations (get, put, size, etc).

ConcurrentHashMap supports full concurrency of retrievals, and adjustable expected concurrency for updates.

No comments:

Post a Comment