Concurrenthashmap (CHM) is a new introduction to Java 1.5 as an alternative to Hashtable, and is an important member of the concurrent package. Before Java 1.5, if you wanted to implement a map that could be used securely in multi-threaded and concurrent programs, you could only choose between hashtable and synchronized maps, because HashMap is not thread-safe. But after we introduced the CHM, we had a better choice. CHM is not only thread-safe, but also better than Hashtable and Synchronizedmap performance. Relative to Hashtable and Synchronizedmap locked the entire map,chm only locked part of the map. CHM allows concurrent read operations while maintaining data integrity during write operations by synchronizing locks. We've learned the basics of CHM in top 5 Java Concurrent collections from JDK 5 and 6, and in this blog I'll introduce the following points:
- How the CHM is implemented in Java
- Under what circumstances should I use a CHM
- Examples of using CHM in Java
- Some important characteristics of CHM
Implementation of Concurrenthashmap in Java
The CHM introduces the segmentation and provides all the features supported by the Hashtable. In CHM, the map is supported by multithreading and does not require any blocking. This benefits from a CHM that divides the map into different parts and locks only a part when performing an update operation. Depending on the default concurrency level (concurrency levels), the map is split into 16 parts and controlled by different locks. This means that you can have up to 16 write threads to manipulate the map at the same time. Imagine that the performance improvement is evident by the ability to enter only one thread into the same time that can be entered simultaneously by 16 write threads (read threads are almost unrestricted). However, because of some update operations, such as put (), remove (), Putall (), clear () only locks the part of the operation, the retrieval operation is not guaranteed to return the latest results.
Another important point is that when iterating through a CHM, the iterator returned by keyset is weakly consistent and fail-safe and may not return some of the most recent changes, and in the traversal process, if the contents of the array that have been traversed have changed, Concurrentmodificationexceptoin exceptions are not thrown.
The default concurrency level for CHM is 16, but can be changed through constructors when creating a CHM. There is no doubt that the concurrency level represents the number of concurrent update operations, so if only a few threads update the map, it is recommended that you set a low concurrency level. In addition, the CHM uses a reentrantlock to lock the segments.
Examples of Concurrenthashmap putifabsent methods in Java
Most of the time we want to insert elements when the element doesn't exist, we typically write the code as follows
Synchronized (map) {
if (map.get (key) = null) {return
map.put (key, value);
} else{return
map.get (key) ;
}
}
The above code is useful in HashMap and Hashtable, but there is a risk of error in the CHM. This is because the CHM does not lock the entire map in the put operation, so when one thread is k,v, another thread calls get null, which causes the value of the put on one thread to be overwritten by the value of the other thread. Of course, you can encapsulate the code into a synchronized code block so that while the thread is safe, it will make your code a single-threaded one. The Putifabsent (Key,value) method provided by CHM implements the same function atomically, while avoiding the risk of the above thread competition.
When to use Concurrenthashmap
CHM is applicable to the number of readers more than the writer, when the number of writer is greater than equal to the reader, CHM performance is lower than Hashtable and synchronized map. This is because when the entire map is locked, the read operation waits for the thread that writes the same part to end. CHM is suitable for cache, initialized at the start of program, and can be accessed by multiple request threads. As Javadoc explains, CHM is a good alternative to Hashtable, but keep in mind that CHM is slightly less synchronized than Hashtable.
Summarize
Now that we know what concurrenthashmap is and when it's time to use Concurrenthashmap, let's review some of the key points of the CHM.
- CHM allows concurrent read and thread-safe update operations
- The CHM locks only part of the map while performing the write operation.
- Concurrent updates are implemented internally by splitting the map into small portions based on the concurrency level.
- High concurrency levels can lead to waste of time and space, and low concurrency levels can cause competition between threads when writing line Chengdo
- All operations of a CHM are thread safe
- The CHM returns an iterator that is weakly consistent, fail-safe and does not throw concurrentmodificationexception exceptions
- CHM does not allow null key values
- You can use CHM instead of Hashtable, but remember that CHM does not lock the entire map
The above is the Java CHM implementation and use of the scene, hope to help everyone! Thank you for your support to this site!