"JDK Source Series" Concurrenthashmap

Source: Internet
Author: User
Tags key case rehash

The key realization of CONCURRENTHASHMAP: segmented lock, simply to divide the hash table into a piece of the table, respectively, and lock. Essentially Concurrenthashmap and HashMap are no different, elements are entry, a node plus a list. In the event of conflict, the use of the chain table method to store elements, so the source will have a large number of hash value through the key to find the slot after the operation of the traversal query.

Here is the source code implementation of the child table:

A segment class has been customized, which inherits the Reentrant lock. You can look at the overall structure:

The more critical one is the Put,rehash,scanandlock,remove,replace method.

Let's first look at the put method of segment:

To do a put operation on one of the segment in the map, join us to achieve a general consideration of the points:

    1. Need to lock, because to consider the same key case, if not locked, two threads simultaneously update the value of the same key, there will be an error.
    2. You need to consider the situation where the lock has been accounted for.
    3. The number of elements that need to be updated segment.
    4. The problem of segment expansion needs to be considered.

With these considerations, we can look at the source code will be more targeted.

The first step is to lock, here directly with the Trylock, because segment inherited the reentrant lock, in fact, this is a very garbage design, it should be preferable to use the combination rather than inheritance. We can see that after the attempt to acquire the lock fails, the Scanandlockforput method is called, and the method tries to acquire the lock continuously, and then matches the entry key value until it succeeds and returns to the Put method.

The Put method takes into account the problem of expansion:

If the number of elements is greater than the threshold value, the Rehash method is called. Rehash the expansion of the array first:

At the same time, the old elements were migrated with rehash operation : idx = e.hash&sizemask

The last put method updates the total number of elements:

Let's take a look at the Remove method:

In the same way, we first think about what points we need to consider:

    1. Need yoke While considering lock being preempted, same as inserting
    2. Update the number of elements

The first is to try to acquire the lock, if the lock is taken up, then wait to get it.

Find the entry first, and then traverse the linked list:

After the element is found, the normal list is removed, and the total number of elements is updated.

There's a strange place here, Modecount. Why did you add one? This is followed by a solution.

The Replace and remove operations are almost identical and are not mentioned here.

"JDK Source Series" 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.