ConcurrentHashMap: concurrenthashmap

Source: Internet
Author: User

ConcurrentHashMap: concurrenthashmap

The integration is frequently used in projects. The most primitive is the Array. The collection List provides convenience and extension for addition and deletion, and for faster search efficiency, select the Map ing table. HashMap is the most commonly used in Map. It provides a better query speed, with the time complexity at O (1 ). These are common collections. However, concurrency is often required. For the Map class, there is a HashTable (optional), which implements the ing function of key-value pairs and also supports concurrent operations. The principle of HashTable implementation is to apply a lock to hashMap and lock it when operating on hashtable. This greatly reduces the efficiency. Low concurrency may not have a huge impact. If hundreds of thousands of concurrent accesses at the same time, it will be too costly to wait.

Based on the above problems, the java. util. concurrent package provides a better set of concurrent ing classes ConcurrentHashMap, which can be literally understood as a concurrent hash ing table. So why is the efficiency of this class so high as HashTable? Let's analyze the ConcurrentHashMap structure.

 

The class contains two important internal classes. One is a Segment, which can be understood as a Segment. Each Segment contains a key-value pair.

A HashEntry stores key-value pairs.

The implementation principle is that the entire class is not locked every time the data is put. First, obtain the hash value of the key, locate the corresponding Segment based on the hash value, and then lock the segment. In this way, when multiple threads operate on the set, if the elements are not in the same Segment, they will not be blocked, which is naturally more efficient. The Segment implementation is similar to HashMap.

The data is retrieved along the access path. First, find the specific Segment, and then find the key-Value Pair Based on the hash value in the Segment.

This is a preliminary understanding of ConcurrentHashMap, and the source code will be analyzed later.

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.