Understanding of Java Concurrenthashmap

Source: Internet
Author: User

HashMap is an unsafe class, Java5.0 uses a synchronous lock to encapsulate the hashmap, but there is a drawback that it will serialize all accesses, which can seriously reduce concurrency, and when multiple threads compete for a lock on a container, throughput will be severely degraded.

Instead of synchronizing each method in the same lock, Concurrenthashmap uses a finer-grained lock-in to achieve greater sharing, a mechanism called segmented locks.

It can be seen from the source code of concurrent that the read operation is not locked.

Next look at the value of node in map, you can see that there are four values from the source code,


Hash,key, Val, Next, where key, hash is immutable, once initialized, it can not be modified, so as to prevent the structure of the broken table, that Concurrenthashmap is how to ensure that the normal writing, which is the use of a segmented lock.

Can be seen from the source code, is the first to read out the segment, and then segment lock, so as to achieve segmented access.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Understanding of Java Concurrenthashmap

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.