Concurrenthashmap principle Analysis of Java concurrent programming

Source: Internet
Author: User

Objective:
Collections are the most commonly used data structures in programming. When it comes to concurrency, it is almost always supported by a collection of such advanced data structures. For example, two threads need to access an intermediate critical section (Queue) at the same time, such as caching as a copy of an external file (HASHMAP). This article focuses on the concurrenthashmap of the 3 concurrent collection types (concurrent,copyonright,queue) in jdk1.5, allowing us to understand them in a detailed and theoretical way, allowing us to benefit from deep project development.
Before Tiger, one of the data structures we used the most was HashMap and Hashtable. As we all know, HashMap is not synchronized, while Hashtable uses synchronized, the direct impact is optional, we can use the HashMap in single-threaded to improve efficiency, and multi-threading with Hashtable to ensure security.
When we enjoy the convenience of the JDK we also bear the unfortunate consequences it brings. Through analysis Hashtable know, synchronized is for the entire hash table, that is, each time lock the entire table to allow the thread monopoly, security behind is a huge waste, the eye unique Doug Lee immediately took out the solution--concurrenthashmap.

On the left is the implementation of the Hashtable-lock the entire hash table, while the right side is the implementation of the Concurrenthashmap-lock bucket (or segment). Concurrenthashmap divides the hash table into 16 buckets (the default), and common operations such as Get,put,remove lock only the buckets that are currently needed. Imagine that the original can only one thread into, now can simultaneously 16 write threads (write threads need to lock, and read threads are almost unrestricted, then mentioned), concurrency is obvious.
Even more surprising is the read concurrency of Concurrenthashmap, because the lock is not used for most of the reads, so the read operation is almost entirely concurrent, and the write operation lock is very granular and much faster than before (this is more noticeable in buckets). You need to lock the entire table only if you are working with size. In the iteration, Concurrenthashmap uses a different iteration of the fast-failing iterator that differs from the traditional collection (see the previous article, "JAVA API Memo-Collection"), which we call a weakly consistent iterator. In this iterative approach, when iterator is created, the collection is no longer thrown concurrentmodificationexception and replaced by new data at the time of the change without affecting the original data. The iterator is completed and then replaced with the new data, so that the iterator thread can use the original old data, and the write thread can also be the concurrent completion of the change, more importantly, this ensures that multiple threads concurrent execution continuity and extensibility, is the key to performance improvement.
Next, let's take a look at several important methods in the Concurrenthashmap, in the heart know the implementation mechanism, use up to be more emboldened.
Concurrenthashmap main entity class is three: Concurrenthashmap (whole hash table), Segment (bucket), Hashentry (node), corresponding to the above diagram can see the relationship between.
Get method (Note that here the method of analysis is for the bucket, because the biggest improvement of CONCURRENTHASHMAP is to refine the granularity to the bucket), first determine whether the current bucket of data number is 0, for 0 naturally not get to what, only return null, This avoids unnecessary searches and avoids mistakes at the lowest cost. Then get the head node (the method will be covered below) is based on the hash and key to determine whether it is the specified value, if it is and the value is not NULL to find, direct return; The program is very simple, but there is a confusing place, this return Readvalueunderlock (E What is it used for? Study its code and return a value after it is locked. But there's already a V V = e.value Gets the value of the node, is this return Readvalueunderlock (e) superfluous? In fact, this is entirely for the sake of concurrency, where V is empty, it is possible that a thread is changing the node, and the previous get operation is not locked, according to the Bernstein condition, read write or write after reading will cause inconsistent data, so here to re-lock this E to read again, To ensure that the correct value is obtained, it is obliged to admire Doug Lee's rigor of thinking. The entire get operation is locked in very few cases, and the concurrency is unavoidable relative to the previous hashtable.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Concurrenthashmap principle Analysis of Java concurrent programming

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.