Concurrent thematic (six) HashMap and ConcurrentHashMap

Hash table

1 Introduction

In the hash table is a kind of bond - the structure value (key-indexed) for storing data, we find the value to be input, i.e., as long as the key, to find the corresponding value.

Hash idea is simple, if all keys are integers, then you can use a simple unordered array to achieve: the key as an index, that is, the value corresponding to its value, the value so that you can quickly access any key . This is a simple case of bonds, we can extend it to handle more complex types of bonds.

2. chained hash table

He said chain hash table is composed of a set list from the root. Each list can be seen as a "bucket", we will all elements by way hash into various concrete buckets. Insert element, which is key to pass a first hash function (a process known as hash key), which elements function tells "bucket" manner by hashing belongs, and then inserted in the corresponding list head element. Locating or deleting elements, with the same elements found their way to the "bucket", and then traverse the corresponding list until you find the elements we want. Because each "bucket" is a chain, so the chain does not limit the number of hash table contains elements. However, if the table becomes too large, its performance will be reduced.

3. scenarios

The core of our well-known caching techniques (such as redis, memcached) is actually maintaining a huge hash table in memory, as well as well-known HashMap, CurrentHashMap and other applications.

ConcurrentHashMap difference and HashMap, etc.

1.HashMap

We know that the underlying HashMap list is an array + to achieve, is not thread-safe in a multithreaded environment, use Hashmap be put action will cause an infinite loop, resulting in CPU utilization close to 100%, so you can not use a HashMap in concurrency.

2.HashTable

The principle HashTable and HashMap is almost the same, the difference is nothing more than

  •  HashTable not allow key and value is null, and HashMap are based on obsolete Dictionary class.
  •  HashTable are thread-safe

But HashTable thread-safe policy implementation costs are too great, simple and crude, get / put all operations are synchronized, which is equivalent to the entire hash table plus a big lock.

Multi-threaded access, as long as there is one thread to access or manipulate the object, that other threads can only be blocked, the equivalent of all the operations serialized in the competitive scenario of concurrent performance will be very poor.

3.ConcurrentHashMap

Mainly in order to deal with hashmap in a concurrent environment of insecurity born, ConcurrentHashMap the design and implementation of very compact, a lot of use of volatile, final, CAS and other lock-free techniques to reduce the impact of lock contention for performance.

We all know Map + array are generally linked list structure (JDK1.8 the array + red-black tree).

 

ConcurrentHashMap avoids global locking into a partial lock operation, thus greatly improving the operating speed of concurrent environment, due to the very different ConcurrentHashMap in JDK1.7 and realization of 1.8, then we talk about in JDK 1.7 and the difference between the 1.8.

The principle of JDK1.7 version of CurrentHashMap

In JDK1.7 array employed in ConcurrentHashMap + Segment + segmented locking manner. 

1.Segment (lock segment)

ConcurrentHashMap the lock segment called Segment, i.e. it is similar to the structure of the HashMap, i.e. has an internal Entry array, each element of the array is a linked list, but it is also a ReentrantLock (Segment inherited ReentrantLock).

2. Internal structure

ConcurrentHashMap lock technique using segmented, data segments by the memory, and to each piece of data with a lock, when a thread holding the lock access data wherein a time segment, the other segment of data can also be accessed by other threads, can be true concurrent access. ConcurrentHashMap is below the internal structure:

Concurrent Programming Series: The principle of ConcurrentHashMap (JDK1.7 and JDK1.8)

From the above we can understand the structure, ConcurrentHashMap a positioning element of the process requires two Hash operation.

The first Hash locate the Segment, second Hash positioned to head the list of elements located.

3. The advantages and disadvantages of the structure

harm

A side effect of this kind of structure is the Hash process longer than ordinary HashMap

benefit

When the write operation can only Segment where the elements can be locked and will not affect other Segment, so, in the best case, ConcurrentHashMap can simultaneously support up to the number of write operations Segment size (just write these are very evenly distributed across all of the Segment).

So, by this kind of structure, ConcurrentHashMap concurrent capacity can be greatly improved.

The principle of JDK1.8 version of CurrentHashMap

In the reference implementation JDK8 ConcurrentHashMap JDK8 HashMap, using the implementation list array + + red-black tree to design, extensive use of internal CAS operation.

JDK8 completely abandoned in favor of a Segment Node, its design concept is no longer segmented lock Thought JDK1.7.

Node: a data structure stored hash value of the key, value and the key. Where value and next are modified with volatile to ensure the visibility of concurrency.

Java8 ConcurrentHashMap and Java8 structure is basically the same as the HashMap, but to ensure thread safety.

 

In the structure JDK8 in ConcurrentHashMap, since the introduction of the red-black tree, so as to achieve ConcurrentHashMap is very complicated, as we all know, red-black tree is a very good performance in a binary search tree, look for its performance O (logN), but its implementation process is very complex, but also very poor readability, Doug
Lea's thinking ability is definitely not an ordinary person can be compared, Map of fully linked list structure of early search time complexity is O (N), JDK8 in the list ConcurrentHashMap a length greater than a certain threshold when the list will be converted into a red-black tree to find further improve its performance.

Concurrent Programming Series: The principle of ConcurrentHashMap (JDK1.7 and JDK1.8)

to sum up

In fact, it can be seen ConcurrentHashMap JDK1.8 version data structure close to the HashMap, relatively speaking, only increased ConcurrentHashMap to control the concurrent operation of the synchronization, the JDK1.7 version ReentrantLock + Segment + HashEntry, the version JDK1.8 synchronized + CAS + HashEntry + red-black tree.

1. The data structure: Segment data structure to cancel the lock segments, is replaced by a linked list structure array + + red-black tree.
2. ensure thread safety mechanism: JDK1.7 use of sub-locking mechanism to achieve thread-safe segment, which segment inherited from ReentrantLock. JDK1.8 using CAS + Synchronized thread-safe.
3. The lock granularity: Segment of the original data needs to be operated lock, is adjusted for each array element lock (Node).
4. converted to red-black tree list: positioning node hash algorithm simplification brings disadvantages, increasing Hash collision, and therefore the number of nodes is greater than 8 list, the list will be stored as the converted red-black tree.
The query time complexity: traversing the list from the original O (n), becomes a red-black tree traversal O (logN).

Red-black tree

Red-black tree is a tree structure, but it is a binary tree (each node can have up to two children nodes, the parent node or less left node, right node is greater than a parent node), in order to ensure the tree opposite left and right child tree balance (the same depth), the red-black tree node using color-coded manner, node marked in red or black, when calculating the depth of the tree nodes only count the number of black, red without counting the number of nodes.

 

Published 22 original articles · won praise 31 · views 110 000 +

Guess you like

Origin blog.csdn.net/a2267378/article/details/104314027