"Java concurrency Programming" 23, Concurrenthashmap Principle Analysis (version 1.7 vs. 1.8)

Source: Internet
Author: User
Tags cas

JDK version 1.8

The implementation of CONCURRENTHASHMAP in 1.8, almost all of the 1.7 version has been changed. First, the data structure of the segment segment lock is canceled, and the structure of the array + linked list (red-black tree) is replaced. For the granularity of the lock, adjust the locking (Node) to each array element.

The steps for put are as follows:

      1. Parameter Validation.
      2. If table[] is not created, it is initialized. Table initialization and expansion using CAS lock-free design, through the state Sizectl to control the concurrent operation of the thread: U.compareandswapint (this, Sizectl, SC,-1)
      3. When there is no node behind table[i], create node directly (no lock operation). Also implemented by CAS: Return U.compareandswapobject (tab, ((Long) I << Ashift) + abase, C, v);
      4. If you are currently expanding, help expand and return to the latest table[].
      5. Then append nodes to the linked list or red-black tree. This process uses synchronized (f) to lock the current node for thread safety
      6. If it is a linked list, traverse the linked list, find the same key replace the old value, do not find the same key is added to the end of the list
      7. If it's a red and black tree,
      8. Finally go back to determine whether to reach the threshold, such as arrival into the red and black tree structure. Method of Use: Treeifybin (tab, i);

The Get () method does not have a lock operation, the steps are as follows:

      1. First, locate the I in table[].
      2. If Table[i] exists, continue to find.
      3. First, the linked list header is compared, and if yes it is returned.
      4. Then, if it is a red-black tree, look for the tree.
      5. If it is not a red-black tree, loop-linked list lookup.

The detailed information on the reference network is as follows:

JDK1.8 realization of Concurrenthashmap

Java concurrency Programming 23, Concurrenthashmap Principle Analysis (version 1.7 and 1.8 contrast)

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.