码迷,mamicode.com
首页 > 编程语言 > 详细

Java并发编程-ConcurrentHashMap

时间:2015-06-05 13:48:27      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:

特点:

将桶分段,并在某个段上加锁,提高并发能力

源码分析:

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 = e.value;
                    if (!onlyIfAbsent)
                        e.value = value;
                }
                else {
                    oldValue = null;
                    ++modCount;
                    tab[index] = new HashEntry<K,V>(key, hash, first, value);
                    count = c; // write-volatile
                }
                return oldValue;
            } finally {
                unlock();
            }
        }

参考文章:http://www.iteye.com/topic/344876

Java并发编程-ConcurrentHashMap

标签:

原文地址:http://www.cnblogs.com/tangyanbo/p/4554223.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!