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