Die knock Java Concurrency "-----j.u.c java concurrency container: concurrenthashmap__java

Source: Internet
Author: User
Tags static class volatile
die knock java concurrency "-----j.u.c java Concurrency container: concurrenthashmap

HashMap is a collection that we use very frequently, but because it is not thread-safe, in a multithreaded environment, put operations are likely to produce a dead loop, resulting in CPU utilization approaching 100%. To solve this problem, two solutions are provided for Hashtable and Collections.synchronizedmap (HASHMAP), but both are lock-and-write, exclusive, one thread must wait while reading, the throughput is low, Performance is relatively low. So Doug Lea has provided us with a high-performance thread-safe hashmap:concurrenthashmap. the realization of Concurrenthashmap

Concurrenthashmap as a concurrent family, it has efficient concurrent operation, compared to Hashtable heavy, Concurrenthashmap is more than a chip.

Before the 1.8 version, Concurrenthashmap used the concept of segmented locks to make the lock more refined, but 1.8 has changed the idea, but the use of cas+synchronized to ensure the security of concurrent updates, of course, the underlying use of the array + linked list + red-black tree storage structure.

About the difference between 1.7 and 1.8 please refer to the small Wolf blog: Talk about the different implementations of ConcurrentHashMap1.7 and 1.8: HTTP://WWW.JIANSHU.COM/P/E694F1E868EC

We have a comprehensive understanding of how concurrenthashmap is implemented in 1.8: Important concepts important internal class CONCURRENTHASHMAP initialization put operation get operation size operation expansion Red black tree conversion important concept

The CONCURRENTHASHMAP defines several constants as follows:

Maximum capacity: 2^30=1073741824 private static final int maximum_capacity = 1 << 30;

The default initial value must be 2 screen number private static final int default_capacity = 16;

static final int max_array_size = integer.max_value-8;

private static final int default_concurrency_level = 16;

Private static final float load_factor = 0.75f;

Linked list to red-black tree valve value,> 8 linked list to red black tree static final int treeify_threshold = 8; Tree chain table valve value, less than or equal to 6 (Tranfer, LC, hc=0 two counters respectively + record the original bin, the number of new Bintreenode, <=untreeify_threshold untreeify (lo)) static

Final int untreeify_threshold = 6;

static final int min_treeify_capacity = 64;

private static final int min_transfer_stride = 16;

private static int resize_stamp_bits = 16;

Maximum number of threads for 2^15-1,help RESIZE private static final int max_resizers = (1 << (32-resize_stamp_bits))-1;

Offset in 32-16=16,SIZECTL record size size private static final int resize_stamp_shift = 32-resize_stamp_bits; 

Forwarding nodes hash value static final int moved =-1; hash value of root node static final int TREebin =-2; 

Reservationnode hash value static final int RESERVED =-3;
      
      
       
       Number of available processors static final int ncpu = Runtime.getruntime (). Availableprocessors ();
       
       1 2 3 4 5 6 7 8 9 10 11 12 13 14 1
       
       5 16 17 18 19 20 21
       
       22 23 24 25 26 27 28
       
       29 30 31 32 33 34 35
       
       36 37 38 39 40 41 42
       
    43 44 45 46   47
      
       

Above is the CONCURRENTHASHMAP definition of the constant, simple and understandable, not much elaboration. Here are some important concepts of concurrenthashmap. Table: The default is NULL, the default size is 16, and the power of the size is always 2 when the node data is stored. Nexttable: Expansion of the newly generated data, the array is twice times the table; node: nodes, saving KEY-VALUE data structures Forwardingnode: A special node node with a hash value of-1, where the nexttable reference is stored. Forwardingnode only works when the table is expanded, as a placeholder to indicate that the current node is null or has been moved SIZECTL: A control identifier that controls table initialization and expansion operations. There are different uses in different places, and their values are different, and they mean different meanings.
A negative number represents an initialization or expansion operation-1 is initializing-n indicates that there are N-1 threads in the capacity expansion operation positive or 0 represents the hash table has not been initialized, this value represents the initialization or next expansion of the size of the important internal class

In order to implement Concurrenthashmap,doug Lea provides a number of internal classes for secondary implementations, such as Node,treenode,treebin and so on. Let's take a look at several important inner classes of concurrenthashmap. Node

As the core and most important internal class in Concurrenthashmap, node plays an important role: Key-value key value pairs. All of the data inserted into the CONCURRENTHASHMAP will be wrapped in node. The definition is as follows:

    Static Class Node<k,v> implements map.entry<k,v> {final int hash;
        Final K Key;             Volatile V Val;    With volatile to ensure visibility of volatile node<k,v> next;
            Pointer node of the next node (int hash, K key, V Val, node<k,v> next) {This.hash = hash;
            This.key = key;
            This.val = val;
        This.next = Next;
        Public final K Getkey () {return key;}
        Public Final v. GetValue () {return val;}
        Public final int hashcode () {return Key.hashcode () ^ Val.hashcode ();}
        Public final String toString () {return key + "=" + val;}
        /** does not allow modification of value of value */public final V SetValue (v value) {throw new unsupportedoperationexception (); Public final Boolean equals (object o) {object k, V, u;
            map.entry<?,? > E; Return ((o instanceof map.entry) && (k = (E = (map.entry<?,? >) o). Getkey ())!= null && (v = e.getvalue ())!= null && (k = = Key | |
        K.equals (Key)) && (v = (U = val) | | v.equals (u));
            /** Assignment Get () method * * node<k,v> find (int h, Object K) {node<k,v> E = this;
                    if (k!= null) {do {k ek; if (E.hash = h && (ek = e.key) = = k | |
                        (Ek!= null && k.equals (EK)))
                return e;
            while ((e = e.next)!= null);
        return null;
       
       } 1 2 3 4 5
       
       6 7 8 9 10 11 12
    13 14 15 16 17   
       18 19 20 21 22 23 24
       
       25 26 27 28 29 30
       
       31 32 33 34 35 36 37
       
        38 39

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.