Multithreading (xiv, ConcurrentHashMap principle of a node)

Introduction ConcurrentHashMap

For JDK1.8 see how ConcurrentHashMap is achieved

Schematic:

Multithreading (xiv, ConcurrentHashMap principle of a node)

1, a table is an array of internal ConcurrentHashMap Node node, each position in the array of table [I] represents a barrel. The mapped to different hash value of the key barrel.

transient volatile Node<K,V>[] table;

2, Node Five types of nodes a

1, Node node, is the parent of all the nodes, it can be placed in the barrel alone or as a head of the list into the barrel.
2, TreeNode node inherits from Node, the red-black tree is a node, the node can not be directly into the barrel, only as a node red-black tree.
3, TreeBin node, the proxy node TreeNode, can be placed in the barrel, may be connected below the root node of the red-black tree, so called proxy node TreeNode.
4, ForwardingNode node, the node expansion, except in the expansion node stage, after the expansion is complete current bucket, the bucket will be placed in the node, then the query will jump to the query expansion table, not the actual data storage
5, ReservationNode node, use internal methods, you can ignore.

2.1 Node node

The default node is Node node on the barrel.
When a hash collision occurs, the Node will first node links to form a chain of Table, when the number of nodes exceeds a certain number, the list will be converted to red-black tree. Because the list to find the average time complexity is O (n), the red-black tree is a balanced binary tree, which is the average time complexity is O (logn).

Node only a next pointer is a single linked list, the list provides a way to achieve find query
Multithreading (xiv, ConcurrentHashMap principle of a node)

Multithreading (xiv, ConcurrentHashMap principle of a node)

2.2 TreeNode node

TreeNode node is red-black tree, TreeNode not be directly linked to table [i] - above the tub, but by TreeBin link, TreeBin points to the root of red-black tree.
Multithreading (xiv, ConcurrentHashMap principle of a node)
FindTreeNode method provides a tree-based lookup.
Multithreading (xiv, ConcurrentHashMap principle of a node)

2.3 TreeBin node

TreeBin directly linked to table [i] - above the tub, the node provides a series of operations related to red-black tree, and locking, unlocking operation.
Multithreading (xiv, ConcurrentHashMap principle of a node)
Further TreeBin offers a range of operation
1, TreeBin (TreeNode <K, V> b), will be the first node b is converted into a red-black tree list.
2, lockroot (), on the red-black tree root write-lock
3, unlockRoot (), release the write lock
4, find (int h, Object k), start from the root node traversal search, find "equal" node returns it, did not find returns null, when there write lock to list ways to find, do not block read locks.
Multithreading (xiv, ConcurrentHashMap principle of a node)
5, removeTreeNode (TreeNode <K, V> p), remove red-black tree node:

  1. Red-black tree size is too small, returns true, then the tree -> the list conversion
  2. When sufficient red-black tree size, without changing into a linked list, but write-lock required when deleting nodes
    6, red-black tree left-handed, right-handed and a series of algorithms.

2.4 ForwardingNode node

1, ForwardingNode a temporary node appears in the ongoing expansion, hash value is fixed to -1, and does not store the actual data.
2, if a hash bucket all nodes are migrated old table array to a new table, then place a ForwardingNode in this bucket.
3, the read operation when confronted ForwardingNode, will be forwarded to the new operating table array expansion up after execution; write operation ran into it, then try to help the expansion, expansion of the expansion is to support multi-threaded together.
4, a method of find to find on the new array nextTable
Multithreading (xiv, ConcurrentHashMap principle of a node)

2.5 ReservationNode node

1, retention node.
2, the hash value is fixed at -3, the actual data is not saved
3, only acts as a placeholder and locked in computeIfAbsent compute two functions in the API formula

Guess you like

Origin blog.51cto.com/janephp/2412917