[JDK source code series] ConcurrentHashMap, concurrenthashmap
Concurrency is always a high-performance topic, and Concurrent containers are an important concurrency tool in java. So let's analyze the ConcurrentHashMap (Chashmap) in the Concurrent package today ). The normal container performs poorly in some concurrent situations. If the container is large in size and the container performs a very time-consuming traversal operation after obtaining the lock, the lock will be occupied for a long time in the world. In fact, only a small part of elements are occupied by threads, and other elements can be fully read and written. However, Chashmap (hereinafter referred to as Chashmap) achieves this feature. When using a part of the container, other threads can read and write the remaining areas of the container at the same time, So Chashmap is highly efficient.
The key implementation of Chashmap is a multipart lock mechanism. In simple terms, it is to divide the hash table into segments and subtables and lock them separately. In this way, the thread will obtain the lock for the segment to be occupied, and the remaining segment will not be affected. Essentially, there is no difference between Chashmap and hashMap. Each element is an Entry and a node is linked. When a conflict occurs, the linked list is used to store elements. Therefore, the source code will find a large number of slots through the hash value of the key and then traverse and query the elements. The sub-Table declaration is as follows:
A custom segment class inherits the reentrant lock. Let's take a look at the overall structure:
Among them, put, rehash, scanandlock, remove, replace, and other methods are critical.
Let's first look at the put Method of segment:
To perform the put operation on a segment in map, we generally consider the following points:
A lock is required because an error occurs when two threads update the value of the same key at the same time if the key is the same.
1) consider the situation where the lock has been occupied.
2) Update the number of segment elements.
3) You need to consider the segment resizing problem.
With these considerations, we may look at the source code more specifically.
The first step is actually locking. Here trylock is used directly, because segment inherits the reentrant lock. In fact, this is a very junk design. It is reasonable to give priority to combination rather than inheritance. We can see that the scanAndLockForPut method is called after a failed attempt to obtain the lock. This method will constantly try to obtain the lock and match the key value of the entry until it succeeds, and return to the put method again. The expansion problem is taken into account in the Put method:
If the number of elements exceeds the threshold, the rehash method is called. Rehash first expands the array:
During the migration of old elements, the rehash operation is performed: idx = e. hash & sizeMask
The put method updates the total number of elements:
Next let's take a look at the remove Method:
1) Let's first think about what needs to be considered:
2) The lock must be shackled, and the lock must be preemptible.
3) Number of updated Elements
First, try to get the lock. If the lock is occupied, wait for it to be obtained.
Obtain the Entry before searching, and then traverse the table:
After the elements are found, remove the elements from the common linked list and update the total number of elements.
This is a bit strange. Why does modeCount add one? This will be answered later.
The Replace and remove operations are almost the same.
After reading the main segment method, let's take a closer look at the Chashmap method. The first is the put method:
First, you need to obtain the corresponding segment and then delegate it to the specific segment. Here is the details. If the value is null, an exception is thrown.
The remove method is also a delegate execution:
'
From the code above, we can see that both the put and remove methods are delegated to the underlying segment for execution, so it is not complicated, but the size method requires statistics because it is complicated to consider the situation of changing elements concurrently. Several Considerations for the Size Method
How can I detect the number of elements in each segment that may be dynamically changed during the statistical process?
What are the criteria for continuous retry statistics? When will the retry be stopped?
What strategy should we adopt if there are too many retries?
The size code shows the answers to the above three questions:
For other similar read operations, such as the contains operation, the element is added/removed during the read period. The same policy is adopted. Read and compare two times. If they are inconsistent, lock them.
Finally, let's take a look at the get method:
There is nothing special about it, that is, a location query.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.