How to use ConcurrentHashMap in Java
Before Java 1.5, if you need Map implementation, which can be safely used in multithreading Java-application, you have only Hashtable or synchronized Map, because HashMap is NOT safe.
ConcurrentHashMap was presented as alternative to Hashtable in Java 1.5 as part of concurrency package. With ConcurrentHashMap, you have a better choice not only if it can be safely used in the concurrent multi-threaded environment but also provides better performance than Hashtable and synchronizedMap. ConcurrentHashMap performs better because it locks a part of Map. It allows concurred read operations and the same time maintains integrity by synchronizing write operations.
In this article we will know:
- How ConcurrentHashMap is implemented in Java.
- Some important properties of ConcurrentHashMap.
- When to use ConcurrentHashMap.
How ConcurrentHashMap is implemented
ConcurrentHashMap was developed as alternative of Hashtable and support all functionality of Hashtable with additional ability, so called concurrency level. ConcurrentHashMap allows multiple readers to read simultaneously without using blocks. It becomes possible by separating Map to different parts and blocking only part of Map in updates. By default, concurrency level is 16, so Map is spitted to 16 parts and each part is managed by separated block. It means, that 16 threads can work with Map simultaneously, if they work with different parts of Map. It makes ConcurrentHashMap hight productive, and not to down thread-safety.
Some important features of ConcurrentHashMap
But you have to consider some particular qualities. Since update operation is not synchronized, simultaneous fetches data may not contains recent updates.
Another thing worth mentioning is iteration over Map. Iterator returned by keySet, inconsistent and has state of ConcurrentHashMap at recent moments in the past and may not contains recent updates. It is fail-safe and does not throw ConcurrentModificationException.
Default level of concurrency is 16, but can be changed during creating ConcurrentHashMap. Since this level is used under the hood and shows number of concurrent updates, in case few updating threads it has the meaning to use low level.
ConcurrentHashMap doesn't allow to keep
putifAbsent(key, value), which allows to add entry into Map, if it is absent yet, but atomically, avoiding race condition. So called “check-if-exists-and-insert” method (get, put) doesn't work for ConcurrentHashMap, because during
put operation whole Map is not locked, and while one thread adds entry, other thread's
get call can return
null, as a result second thread overrides value inserted by first thread. Of course, you can wrap your code in synchronized block and make it thread-safe, but such solution makes your code single-threaded.
When you should use ConcurrentHashMap in Java
ConcurrentHashMap is best solution, when you have many reading threads and several writing ones. In other case, if writing threads more or equal to reading threads, performance of ConcurrentHashMap reduce to level of synchronizedMap and Hashtable. Throughput of ConcurrentHashMap falls, because there is locking all parts of Map, each reading thread is waiting for writing ones, while working with this area of Map.
ConcurrentHashMap is good choice for caches, which can be initialized during starting application and later give access to requesting threads.
ConcurrentHashMap is good replacement for Hashtable and can be used anywhere, but you should keep in mind about features of synchronization ConcurrentHashMap in comparison with Hashtable.
That's all for now.
If you like this article — share it with your friends. This is the best motivation to develop the blog.
Versions of the article
Русская версия: Как использовать ConcurrentHashMap в Java
Don’t hesitate to comment below if you have any questions. Feel free to join the conversation.
This blog is about Java development and describes in detail the most important topics.