Sophisticated back-end notes 10: Analysis ConcurrentHashMap

Before analyzing ConcurrentHashMap, take a look at the source code is the basis of paragraph HashMap what

A, HashMap

1.1 JDK1.7

Overview of the first to look at the structure of the map:

20200320144054

JDK1.7 source code is here to copy down (do not know why in the zip is not the source, but also need to compile, too lazy to toss).

By looking at the properties of HashMap, you can see, there is table (, entrySet) are most likely to be a HashMap, where data is actually stored:

20200319143007

1.1.1 to keep track of what we first constructor:

It can be seen, in addition to the constructor parameter calibration basis, only made the default setting capacity, the threshold factor is set (the so-called threshold factor, that is, when the capacity reaches a certain percentage, is automatic expansion):

20200319165434

1.1.2 Then we look at the put method

Now, we put the method to verify the structure:

20200319172849

1.2 JDK1.8

The JDK1.8 HashMap made relatively large changes, in addition to the performance boost, mainly linked list also been optimized: If the list is too long, it will be optimized to red-black tree, but the main structure of the JDK1.7 HashMap is quite close, it will temporarily expand the analysis. Specific changes and upgrades recommended reference: the Java series of 8 new understanding HashMap .

1.3 HashMap summary

You can see, the basis of paragraph HashMap, is not doing any consideration of thread-safe, that is, in the case of concurrent, we caution use HashMap, Next, take a look at how to do ConcurrentHashMap is thread-safe.

Two, ConcurrentHashMap

2.1 JDK1.7

In JDK1.7, ConcurrentHashMap structure can be said that the structure of the HashMap other day capsule, its structure is as follows: you can see, there are a plurality of segment objects inside ConcurrentHashMap, and HashEntity array segment object data is actually stored is where the maximum number of concurrent ConcurrentHashMap number of objects in the segment, is the current map lock supports.

20200320145239

2.1.1 Constructor

Is also beginning to track the constructor, take a look at the new time, what do we do:

20200320141921

2.1.2 put method

The next step is the most critical putmethod, the Segmentobject tablearray is in the putprocess of rehashautomatic expansion method: It can be seen in operation segment object table (HashEntity array), will be wrapped in lock (tryLock), unlock method in which to achieve a thread-safe, so that the maximum number of concurrent ConcurrentHashMap number of objects in the segment, is the current map lock supports.

20200320110603

2.2 JDK1.8

In jdk1.7, you can see the structure and HashMap ConcurrentHashMap completely different, but in JDK1.8, ConcurrentHashMap be modified into a structure very similar to the HashMap:

20200320212439

Here we will focus on how to sort out what's put in ConcurrentHashMap method JDK1.8 is to achieve thread-safe: you can see, the new node at the time, but the use of lock-free operations CAS mechanism, and in the operation node object that points to the list / when the tree (up to a certain length of the chain will be optimized as a tree), using only synchronized lock. HashMap and the difference is that the new Node object is hung on the end of the list.

20200320211510

2.3 ConcurrentHashMap summary

We can see that both JDK1.7 or 1.8, although using a completely different mechanism, but made thread-safe considerations, of course, bring performance degradation is also reasonable. We now know the internal structure of ConcurrentHashMap, at initialization time, not just out of a new direct everything will be fine, but you can try based on business needs, is to specify a number of initialization parameters, in order to avoid frequent ConcurrentHashMap automatic expansion.

Guess you like

Origin juejin.im/post/5e74c973e51d4526e32c5234