ConcurrentHashMap source plane resolution (based jdk1.7)

Before we look at the source code you must first know what the basic structure of ConcurrentHashMap. ConcurrentHashMap segment is the use of locks to control concurrency.

Wherein an internal class Segment classes are used for the lock. The Segment class have a HashEntry <K, V> [] array that is the real use

To store our key-value is.

Approximately follows the structure of FIG. A Segment array, Segment each element of the array as a HashEntry array

 

Before we look at the source code you must also understand the several default constant values:

  • DEFAULT_INITIAL_CAPACITY = 16 default capacity of the container 16
  • DEFAULT_LOAD_FACTOR = 0.75f ​​default expansion factor is 0.75
  • DEFAULT_CONCURRENCY_LEVEL = 16 default is 16 concurrency
  • MAXIMUM_CAPACITY = 1 << 30 The maximum capacity of the container 1073741824
  • MIN_SEGMENT_TABLE_CAPACITY = minimum segment size 2
  • MAX_SEGMENTS = 1 << 16 The maximum segment size
  • RETRIES_BEFORE_LOCK = 2 does not acquire the lock by way of acquisition attempts size of

 

The default value is well above ConcurrentHashMap defined in the following places many of us will use them.

Start to begin initialization

We are using by ConcurrentHashMap by  ConcurrentHashMap <String, String> map = new ConcurrentHashMap <> (); manner

We point into tracking down the source

/**
     * Creates a new, empty map with a default initial capacity (16),
     * load factor (0.75) and concurrencyLevel (16).
     */
    public ConcurrentHashMap() {
        this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR, DEFAULT_CONCURRENCY_LEVEL);
    }

You can see, the default constructor call another band parametric no-argument constructor, and this is what the constructor parameter passed in when you initialize the matter, will eventually jump to the parameterized constructor.

Point to go see this band-argument constructor function realized what

public ConcurrentHashMap(int initialCapacity,
                             float loadFactor, int concurrencyLevel) {
        if (!(loadFactor > 0) || initialCapacity < 0 || concurrencyLevel <= 0)
            throw new IllegalArgumentException();
        if (concurrencyLevel > MAX_SEGMENTS)
            concurrencyLevel = MAX_SEGMENTS;
        // Find power-of-two sizes best matching arguments
        int sshift = 0;
        int ssize = 1;
        while (ssize < concurrencyLevel) {
            ++sshift;
            ssize <<= 1;
        }
        this.segmentShift = 32 - sshift;
        this.segmentMask = ssize - 1;
        if (initialCapacity > MAXIMUM_CAPACITY)
            initialCapacity = MAXIMUM_CAPACITY;
        int c = initialCapacity / ssize;
        if (c * ssize < initialCapacity)
            ++c;
        int cap = MIN_SEGMENT_TABLE_CAPACITY;
        while (cap < c)
            cap <<= 1;
        // create segments and segments[0]
        Segment<K,V> s0 =
            new Segment<K,V>(loadFactor, (int)(cap * loadFactor),
                             (HashEntry<K,V>[])new HashEntry[cap]);
        Segment<K,V>[] ss = (Segment<K,V>[])new Segment[ssize];
        UNSAFE.putOrderedObject(ss, SBASE, s0); // ordered write of segments[0]
        this.segments = ss;
    }

We see that the constructor is a total of three parameters, namely the initial size of the container, load factor, concurrency, these three parameters a new ConcurrentHashMap if we do not specify,

It will take the default parameters, that is, we say the beginning of this article that several constant values.

Here I explain these three parameters do next. Initializing the entire map size container capacity. Load factor is used to calculate the threshold for each segment in the array HashEntry expansion. Concurrency is

To set the length of the segment of the array.

At the beginning of the two if there's really nothing. It is used to determine the correctness of our arguments passed in. When the load factor, the initial capacity, and not in accordance with specifications of concurrent arithmetic exception will be thrown. When the second time if passed in

Concurrent time is greater than the maximum segment size, it will be set to the maximum segment size.

This is more interesting. Since the required length segment array must be n power of two, when n-th power of our not passed in concurrent 2 calculates the n-th power of its closest value 2

For example, how do we passed in 1415 that concurrency is by calculating the length of the array segment is 16. In the above figure we can see the two local variables and ssize sshift, in the circulation is less than if ssize

Concurrency its binary will be left one, that is, multiplied by two. Thus ssize n-th power is used to save the calculated value closest to our concurrency 2. Ssfhit are used to calculate the offset. Here we

Say two very important global constants. segmentMask and segmentShift. Wherein segmentMask is ssize - 1, since ssize a multiple of two. So segmentMask is odd. Into

Binary 1 is full, and segmentShift to 32 - sshift size. 32 is a key value through the bit and then find out the hash value. segmentMask and segmentShift elements are used to locate the current

In which position the array segment that position, and an array of in HashEntry, we will later explain in detail how counted.

This section of code is used to determine the parameters and initialize the array segment for each segment hashentry inside. The first is to prevent initialization if we set

容量大于最大容量。而c是用来计算每个hashentry数组的容量。由于每个hashentry数组容量也需要为2的n次方,因此这里也需要

一个cap和循环来计算一个2的n次方值,方法和上面一样。这里计算出来的cap值就是最终hashentry数组实际的大小了。

初始化就做了这些工作了。

那么我们在说说最简单的get方法。

get方法就需要用到定位我们的元素了。而定位元素就需要我们上面初始化时设置好的两个值:segmentMask和segmentShift

 上面说了,并发度默认值为16,那么ssize也为16,因此segmentMask为15.由于ssize二进制往左移了4位,那么sshift就是4,

segmentShift就是32-4=28.下面我们就用segmentMask=15,segmentShift为28来说说怎么确定元素位置的。

在这里我们要说下hash值,这里的hash值不是key的hashcode值,而是经过再hash确定下来的一个hash值,目的是为了减少hash冲突。

hash值二进制为32位。

上图两个红框就是分别确定segment数组中的位置和hashentry数组中的位置。

我们可以看到确定segment数组是采用 (h >>> segmentShift) & segmentMask,其中h为再hash过的hash值。将32为的hash值往右移segmentShift位。这里我们假设移了28位。

而segmentMask为15,就是4位都为一的二进制。将高4位与segmentMask相与会等到一个小于16的值,就是当前元素再的segment位置。

确定了所属的segment后。就要确认在的hashentry位置了。通过第二个红框处,我们可以看到确定hashentry的位置没有使用上面两个值了。而是直接使用当前hashentry数组的长度减一

和hash值想与。通过两种不同的算法分别定位segment和hashenrty可以保证元素在segment数组和hashentry数组里面都散列开了。

Put方法

public V put(K key, V value) {
        Segment<K,V> s;
        if (value == null)
            throw new NullPointerException();
        int hash = hash(key);
        int j = (hash >>> segmentShift) & segmentMask;
        if ((s = (Segment<K,V>)UNSAFE.getObject          // nonvolatile; recheck
             (segments, (j << SSHIFT) + SBASE)) == null) //  in ensureSegment
            s = ensureSegment(j);
        return s.put(key, hash, value, false);
    }
final V put(K key, int hash, V value, boolean onlyIfAbsent) {
            HashEntry<K,V> node = tryLock() ? null :
                scanAndLockForPut(key, hash, value);
            V oldValue;
            try {
                HashEntry<K,V>[] tab = table;
                int index = (tab.length - 1) & hash;
                HashEntry<K,V> first = entryAt(tab, index);
                for (HashEntry<K,V> e = first;;) {
                    if (e != null) {
                        K k;
                        if ((k = e.key) == key ||
                            (e.hash == hash && key.equals(k))) {
                            oldValue = e.value;
                            if (!onlyIfAbsent) {
                                e.value = value;
                                ++modCount;
                            }
                            break;
                        }
                        e = e.next;
                    }
                    else {
                        if (node != null)
                            node.setNext(first);
                        else
                            node = new HashEntry<K,V>(hash, key, value, first);
                        int c = count + 1;
                        if (c > threshold && tab.length < MAXIMUM_CAPACITY)
                            rehash(node);
                        else
                            setEntryAt(tab, index, node);
                        ++modCount;
                        count = c;
                        oldValue = null;
                        break;
                    }
                }
            } finally {
                unlock();
            }
            return oldValue;
        }

 

上面两片代码就是put一个元素的过程。由于Put方法里需要对共享变量进行写入操作,因此为了安全,需要在操作共享变量时加锁。put时先定位到segment,然后在segment里及逆行擦汗如操作。

插入有两个步骤,第一步判断是否需要对segment里的hashenrty数组进行扩容。第二步是定位添加元素的位置,然后将其放在hashenrty数组里。

我们先说说扩容。

 

在插入元素的时候会先判断segment里面的hashenrty数组是否超过容量threshold。这个容量是我们刚开始初始化hashenrty数组时采用容量大小和负载因子计算出来的。

如果超过这个阈值(threshold)那么就会进行扩容。扩容括的时当前hashenrty而不是整个map。

如何扩容

扩容的时候会先创建一个容量是原来两个容量大小的数组,然后将原数组里的元素进行再散列后插入到新的数组里。

Size方法

由于map里的元素是遍布所有hashenrty的。因此统计size的时候需要统计每个hashenrty的大小。由于是并发环境下,可能出现有线程在插入或者删除的情况。因此会出现

错误。我们能想到的就是使用size方法时把所有的segment的put,remove和clean方法都锁起来。但是这种方法时很低效的。因此concurrenthashmap采用了以下办法:

先尝试2次通过不加锁的方式来统计各个segment大小,如果统计的过程中,容器的count发生了变化,再采用加锁的方式来统计所有segment的大小。

concurrenthashmap时使用modcount变量来判断再统计的时候容器是否放生了变化。在put、remove、clean方法里操作数据前都会将辩能力modCount进行加一,那么在统计

size千后比较modCount是否发生变化,就可以知道容器大小是否发生变化了。

Guess you like

Origin www.cnblogs.com/czsy/p/10987976.html