特点:

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

源码分析:

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

相关文章:

  • 2022-01-31
  • 2021-08-24
  • 2022-01-09
  • 2021-05-05
猜你喜欢
  • 2021-07-04
  • 2021-11-01
  • 2021-07-03
  • 2021-07-29
  • 2022-12-23
  • 2022-01-09
相关资源
相似解决方案