Implementation of Concurrenthashmap in Java

Source: Internet
Author: User


The implementation of Concurrenthashmap in Java Concurrenthashmap (abbreviated CHM) introduces segmentation and provides all the functionality that Hashtable supports. In the CHM, multithreading is supported to read the map and does not require any blocking. This is due to the CHM splitting the map into different parts and only locking a portion of the update operation. Based on the default concurrency level (concurrency level), the map is divided into 16 sections and is controlled by different locks. This means that at the same time there can be up to 16 write threads to manipulate the map. Imagine that the performance improvement is evident by the ability to enter only one thread at the same time by 16 write threads (almost unlimited read threads). However, due to some update operations such as put (), remove (), Putall (), clear () only the part of the operation is locked, so the retrieval operation is not guaranteed to return the most recent results.

Another important point is that when iterating through the CHM, the iterator returned by keyset is weakly consistent and fail-safe, and some recent changes may not be returned, and if the contents of the traversed array change over the course of the traversal, Concurrentmodificationexceptoin exceptions are not thrown.

The default concurrency level for CHM is 16, but can be changed by constructors when creating a CHM. There is no doubt that the concurrency level represents the number of concurrent update operations, so it is recommended to set a low concurrency level if only a few threads update the map. In addition, the CHM uses Reentrantlock to lock the segments.

When to use Concurrenthashmap

When the CHM is applied to more readers than the writer, the performance of the CHM is lower than Hashtable and synchronized map when the number of the writer is greater than or equal to the reader. This is because when the entire map is locked, the read operation waits for the thread to finish writing to the same part. The CHM is intended to be a cache, initialized at program startup, and can then be accessed by multiple request threads. As Javadoc illustrates, the CHM is a good alternative to Hashtable, but remember that the CHM is slightly less synchronized than the Hashtable.


Implementation of Concurrenthashmap in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.