banner
RustyNail

RustyNail

coder. 【blog】https://rustynail.me 【nostr】wss://ts.relays.world/ wss://relays.world/nostr

【Translation】Difference between ConcurrentHashMap, Hashtable and Synchronized Map in Java

Translated from: Javarevisited's Blog

ConcurrentHashMap vs Hashtable vs Synchronized Map#

Although all three classes can be used in Java concurrent programs with thread safety, they have distinctive differences, mainly in how they achieve their thread safety.

Hashtable appeared in Java 1.1 and uses synchronized methods to achieve thread safety. All methods in Hashtable are synchronized, which makes it slow when the number of threads increases. Synchronized Map is not much different from Hashtable and provides similar performance in concurrent programming. The only difference between Hashtable and Synchronized Map is that the latter can use the Collections.synchronizedMap() method to turn any Map into a synchronized version.

On the other hand, ConcurrentHashMap is specifically designed for concurrent programming. By default, it allows 16 threads to read and write the Map without any external synchronization. Due to the implementation of segmented locks, it has considerable scalability. Unlike Synchronized Map and Hashtable, it does not lock the entire Map, but divides the map into segments and locks them individually. Therefore, its performance in reading (without locking in get) is higher than in writing.

To be honest, I think using the Collections class as a core class in the Java API requires caution and is an art. From my personal experience, using ArrayList instead of unnecessary Vector can improve the performance of Java applications. Before Java 5, the Java Collection framework had a problem with lack of scalability.

In multi-threaded Java applications, Hash table and Vector quickly become bottlenecks. After Java 1.5, scalability was introduced and excellent concurrent containers were recommended, which can maintain high efficiency when dealing with large amounts of data. Legacy systems, such as electronic trading systems, rely on the ability to quickly access stored data (referring to the use of containers that can support fast high concurrency).

In this article, we discussed ConcurrentHashMap, Hashtable, HashMap, and synchronized Map in Java, and explored the differences between ConcurrentHashMap, Hashtable, and synchronized Map. This blog also discussed the differences between Hashtable and HashMap, which can help you answer some questions during interviews.

Why do we need ConcurrentHashMap and CopyOnWriteArrayList#

Synchronized containers, such as Hashtable, Vector, Collections.synchronizedMap(), and Collections.synchronizedList(), provide optional thread-safe implementations of Map or List. However, some factors make them unsuitable for use in highly concurrent programs. For example, their large range of locks hinders their scalability, and locking the entire container is usually required during iteration to prevent ConcurrentModificationException.

The implementations of ConcurrentHashMap and CopyOnWriteArrayList provide higher concurrency while ensuring thread safety, compromising the caller at minimal cost. ConcurrentHashMap and CopyOnWriteArrayList are not necessary everywhere; you can also use HashMap or ArrayList, but only in specific cases (concurrency safety). Using them in many concurrent programs can bring benefits.

Differences between ConcurrentHashMap and Hashtable#

So what are the differences between ConcurrentHashMap and Hashtable? Both can be used in a multi-threaded environment, but when the size of Hashtable becomes larger, its performance will be significantly affected because it locks the entire container when accessing it.

ConcurrentHashMap divides the data into segments, so no matter how large the data is, it only needs to be concerned about the locked range. Under the premise of ensuring thread safety, many other read threads can access the container before the iteration is completed.

In summary, when iterating, ConcurrentHashMap only locks a part of the map, while HashMap locks the entire map. This figure clearly illustrates the internal workings of Java ConcurrentHashMap.

ConcurrentHashMap

Differences between ConcurrentHashMap and Collections.synchronizedMap#

ConcurrentHashMap is designed for concurrency and performance improvement, while HashMap is not synchronized by default. Using synchronized Map can wrap it to make it a synchronized container. There are some differences between ConcurrentHashMap and Collections.synchronizedMap:

ConcurrentHashMap does not allow null keys or null values, while synchronized HashMap allows a null key.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.