hashmap difference in 1.7 and 1.8? concurrenthashmap

hashmap difference in 1.7 and 1.8? #

1.8 concurrenthashmap of knowledge #

Important attributes -sizeCtl

A negative number is initializing operation or expansion
-1 for initializing -N N-1 expressed threads expansion operation being 0 or a positive number representative of the hash table has not been initialized, a size-expansion or under this value represents the initialization,

Some properties

  1. The method of using the volatile value set in the node position
  2. Cas node using the algorithm provided in the i-node position of the node, if it accepts an operation equal
  3. If not the latest value of the current thread, this modification might overwrite the modifications Other threads

forwardingNode

For connecting two nodes of the class table, contains a nextTable pointer points to the next for a table, k this node, v, next all pointer is null, its hash value is -1

Expansion method transfer:

The entire expansion is divided into two parts:

  1. Construction of a nexttable, capacity 2-fold, a single thread is completed (RESIZE_STAMP_SHIFT).
  2. The original copy of the table of the elements to the nexttable, there is multithreading.

Single-threaded operation:

  1. Firstly tabAt method of obtaining the position of the element i
  2. Position is empty, i placed in position in the table node forwardNode
  3. If the node is the node location (fh> = 0), if the head node, construct a list in reverse order, the results are placed in the processing position nexttable i and i + n on.
  4. If the location is treebin node (fh <0), a reverse order to do the processing, and determines whether untreefi (after expansion tree structure is no longer needed, reverse conversion of the linked list structure), respectively, on the processing result of nexttable positions i and i + n on.
  5. Traversing all nodes after completion of the copying operation, this way, as the new nexttable Table, and updates sizeCtl 0.75 times the capacity of a new, complete expansion

Multithreading:

  1. If the nodes are traversed node forward, rearward to traverse mechanism coupled to the node locked, complete control of the multi-threaded, multi-threaded traversing node, the processing of a node is found, the value is set corresponding point forward, another thread to see forward, to traverse backwards, so cross to complete the copy work.

helptransfer
When this method is called, the current concurrenthashmap have nexttable objects, first of all get nexttable object, call transfer method can see that when this thread into the expansion process when it will go directly to the copy phase.

put method

If a tree node, according to a tree inserted, if the list is then inserted at the end

get method

It must satisfy two conditions: key hash value is the same as the same

1.7 concurrenthashmap knowledge

By high n bits of the key (n = 32 - segmentShift) and concurrency runtime minus 1 (segmentMask) do bitwise targeting Segment resides. segmentShift and segmentMask during construction are to be calculated from the respective concurrency level.

get operation

  1. The calculated hash value key
  2. The calculated hash value of the positioning segment, if the segment is not null and is not null segment.table, jump into the inside of the loop

put operation

  1. Determine whether the value is null
  2. Calculated hash value
  3. If the positioning segment does not exist, create
  4. The put call segment methods:
    1, to obtain a lock, to ensure the security thread
    2, to target specific HashEntry
    3, traversing HashEntry list, if the key already exists, then determine the value of the incoming onlyIfAbsent, and then decide whether to overwrite the old value
    last release locks returns the old value

size operation

Unlock the first time, the number of updates of the calculation, the second calculation is not locked, re-record the number of updates, compare the number of update twice, if not the same, the third time is calculated, and the second is the number of updates comparing the third time, if the same count value is returned, if different calculation for all segment lock

important point

  1. concurrenthashmap the key and value can not be null, hashmap the key can be null, hashtable the key can not be null
  2. concurrenthashmap is thread-safe class does not guarantee concurrenthashmap operations are thread-safe
  3. concurrenthashmap the get operation does not require a lock, put the operation need to lock
  4. High quality hash operation, if the hash all stored in the same segment, then use the significance of this class will not be great

The difference between 1.7 and 1.8 in the ConcurrentHashMap

1.7 Segment concurrenthashmap mainly used to reduce the intensity of the lock, the hashmap is divided into several segment, put in the time needed to lock segment, get locked when not in use volatile to ensure visibility when to global statistics (size) We will first try to determine the multiple computing modcount, which a few attempts, if there are other threads has been modified to operate, and if not, then directly back size, if there is a need to lock all of the segment is calculated.
1.7 concurrenthashmap, when a collision is too long will be very frequent, the list of CRUD operations will consume a long time, so 1.8 is completely rewritten, there are a few different things:

  1. Employed without using segment node, node lock to achieve a reduction in the intensity of the lock
  2. Designed moved state, when the resize process, the thread 2 also put data, thread 2 will help resize
  3. Using three cas node operation to ensure some of the atoms of operation, the lock in this way instead of
  4. SizeCtl role of different values ​​to represent different meanings, played a controlled

Concurrenthashmap and the difference between hashmap?

hashmap 1.7 和 1.8

Guess you like

Origin blog.csdn.net/leohu_v5/article/details/83303831