首页 > 代码库 > JDK之ConcurrentHashMap分析

JDK之ConcurrentHashMap分析

对ConcurrentHashMap是一个线程安全的map感到好奇,所以就试着去看了看JDK的源码

ConcurrentHashMap分成一个一个的段。然后每次要put或get元素的时候,就先找到这个段,然后执行put或get。为了线程安全,在put方法里面加了锁

看下segment这个属性

    final Segment<K,V>[] segments;
Segmen里面有下面这个属性

        transient volatile HashEntry<K,V>[] table;
在看下HashEntry

    static final class HashEntry<K,V> {
        final K key;
        final int hash;
        volatile V value;
        final HashEntry<K,V> next;

        HashEntry(K key, int hash, HashEntry<K,V> next, V value) {
            this.key = key;
            this.hash = hash;
            this.next = next;
            this.value = http://www.mamicode.com/value;>
这不就是用来存键值对的么。。。



再来看看ConcurrentHashMap的put方法

    public V put(K key, V value) {
        if (value =http://www.mamicode.com/= null)>就是通过segmentFor方法找到那个segment,然后交给那个segment去进行put操作


在看看Segment类中的put方法

        V put(K key, int hash, V value, boolean onlyIfAbsent) {
            lock();
            try {
                int c = count;
                if (c++ > threshold) // ensure capacity
                    rehash();
                HashEntry<K,V>[] tab = table;
                int index = hash & (tab.length - 1);
                HashEntry<K,V> first = tab[index];
                HashEntry<K,V> e = first;
                while (e != null && (e.hash != hash || !key.equals(e.key)))
                    e = e.next;

                V oldValue;
                if (e != null) {
                    oldValue = http://www.mamicode.com/e.value;>就是先加了个锁,然后通过hash值找到对象的数组下标。然后循环这些hash值相同的e节点,如果key相同就替代它的value值,否则就进行插入的动作。然后手动释放锁。

这个保证了线程的安全,有比hashtable的效率高多了。因为hashtable对它的方法都进行了同步。


ConcurrentHashMap里面有个size方法,也值得看一下。

    public int size() {
        final Segment<K,V>[] segments = this.segments;
        long sum = 0;
        long check = 0;
        int[] mc = new int[segments.length];
        // Try a few times to get accurate count. On failure due to
        // continuous async changes in table, resort to locking.
        for (int k = 0; k < RETRIES_BEFORE_LOCK; ++k) {
            check = 0;
            sum = 0;
            int mcsum = 0;
            for (int i = 0; i < segments.length; ++i) {
                sum += segments[i].count;
                mcsum += mc[i] = segments[i].modCount;
            }
            if (mcsum != 0) {
                for (int i = 0; i < segments.length; ++i) {
                    check += segments[i].count;
                    if (mc[i] != segments[i].modCount) {
                        check = -1; // force retry
                        break;
                    }
                }
            }
            if (check == sum)
                break;
        }
        if (check != sum) { // Resort to locking all segments
            sum = 0;
            for (int i = 0; i < segments.length; ++i)
                segments[i].lock();
            for (int i = 0; i < segments.length; ++i)
                sum += segments[i].count;
            for (int i = 0; i < segments.length; ++i)
                segments[i].unlock();
        }
        if (sum > Integer.MAX_VALUE)
            return Integer.MAX_VALUE;
        else
            return (int)sum;
    }







JDK之ConcurrentHashMap分析