Difference between ConcurrentHashMap and Hashtable

Summary

ConcurrentHashMap and Hashtable both provide thread-safe key-value storage, but their underlying synchronization mechanisms differ significantly. Hashtable ensures thread safety by synchronizing all operations, which locks the entire map during any access. Conversely, ConcurrentHashMap utilizes segmentation and concurrent locks, allowing it to lock only specific portions of the data. This segmented approach enables multiple threads to access different parts of the map concurrently, leading to improved performance. For multithreaded applications managing large datasets, ConcurrentHashMap is the preferred choice due to its more granular and efficient concurrency control.

Both ConcurrentHashMap and Hashtable are Collection classes for storing key value pairs and they both provide fast element search with a supplied key. They have much in common. However, we will not discuss the similarities between them here, instead we will focus on the differences between them.

ConcurrentHashMap and Hashtable are both thread safe. But the mechanism for thread safe is different between them. Hashtable is synchronized, it utilizes the synchronization mechanism; while ConcurrentHashMap uses segmentation to lock the data, it uses concurrent locks operations for thread safety instead of synchronized.

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.

So if you want to store large amount of data in a multithreaded program, you should consider to choose ConcurrentHashMap.

CONCURRENTHASHMAP HASHTABLE

  RELATED

  COMMENTS

3
hi
Nov 22, 2013 at 5:00 am

<script>

alert("Hello");

</script>

hi
Nov 25, 2013 at 9:57 pm

<script>alert("Hello")</script>

Input Validation needed
Nov 25, 2013 at 11:07 pm

Hi,

Input validation is needed for Nickname :)