Map 这样的 Key Value
在软件开发中是很是经典的结构,经常使用于在内存中存放数据。java
本篇主要想讨论 ConcurrentHashMap 这样一个并发容器,在正式开始以前我以为有必要谈谈 HashMap,没有它就不会有后面的 ConcurrentHashMap。node
众所周知 HashMap 底层是基于 数组 + 链表
组成的,不过在 jdk1.7 和 1.8 中具体实现稍有不一样。git
1.7 中的数据结构图:github
先来看看 1.7 中的实现。面试
这是 HashMap 中比较核心的几个成员变量;看看分别是什么意思?数组
table
真正存放数据的数组。Map
存放数量的大小。重点解释下负载因子:安全
因为给定的 HashMap 的容量大小是固定的,好比默认初始化:数据结构
public HashMap() {
this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR);
}
public HashMap(int initialCapacity, float loadFactor) {
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal initial capacity: " +
initialCapacity);
if (initialCapacity > MAXIMUM_CAPACITY)
initialCapacity = MAXIMUM_CAPACITY;
if (loadFactor <= 0 || Float.isNaN(loadFactor))
throw new IllegalArgumentException("Illegal load factor: " +
loadFactor);
this.loadFactor = loadFactor;
threshold = initialCapacity;
init();
}
复制代码
给定的默认容量为 16,负载因子为 0.75。Map 在使用过程当中不断的往里面存放数据,当数量达到了 16 * 0.75 = 12
就须要将当前 16 的容量进行扩容,而扩容这个过程涉及到 rehash、复制数据等操做,因此很是消耗性能。并发
所以一般建议能提早预估 HashMap 的大小最好,尽可能的减小扩容带来的性能损耗。app
根据代码能够看到其实真正存放数据的是
transient Entry<K,V>[] table = (Entry<K,V>[]) EMPTY_TABLE;
这个数组,那么它又是如何定义的呢?
Entry 是 HashMap 中的一个内部类,从他的成员变量很容易看出:
知晓了基本结构,那来看看其中重要的写入、获取函数:
public V put(K key, V value) {
if (table == EMPTY_TABLE) {
inflateTable(threshold);
}
if (key == null)
return putForNullKey(value);
int hash = hash(key);
int i = indexFor(hash, table.length);
for (Entry<K,V> e = table[i]; e != null; e = e.next) {
Object k;
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
}
modCount++;
addEntry(hash, key, value, i);
return null;
}
复制代码
void addEntry(int hash, K key, V value, int bucketIndex) {
if ((size >= threshold) && (null != table[bucketIndex])) {
resize(2 * table.length);
hash = (null != key) ? hash(key) : 0;
bucketIndex = indexFor(hash, table.length);
}
createEntry(hash, key, value, bucketIndex);
}
void createEntry(int hash, K key, V value, int bucketIndex) {
Entry<K,V> e = table[bucketIndex];
table[bucketIndex] = new Entry<>(hash, key, value, e);
size++;
}
复制代码
当调用 addEntry 写入 Entry 时须要判断是否须要扩容。
若是须要就进行两倍扩充,并将当前的 key 从新 hash 并定位。
而在 createEntry
中会将当前位置的桶传入到新建的桶中,若是当前桶有值就会在位置造成链表。
再来看看 get 函数:
public V get(Object key) {
if (key == null)
return getForNullKey();
Entry<K,V> entry = getEntry(key);
return null == entry ? null : entry.getValue();
}
final Entry<K,V> getEntry(Object key) {
if (size == 0) {
return null;
}
int hash = (key == null) ? 0 : hash(key);
for (Entry<K,V> e = table[indexFor(hash, table.length)];
e != null;
e = e.next) {
Object k;
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
return e;
}
return null;
}
复制代码
key、key 的 hashcode
是否相等来返回值。不知道 1.7 的实现你们看出须要优化的点没有?
其实一个很明显的地方就是:
当 Hash 冲突严重时,在桶上造成的链表会变的愈来愈长,这样在查询时的效率就会愈来愈低;时间复杂度为
O(N)
。
所以 1.8 中重点优化了这个查询效率。
1.8 HashMap 结构图:
先来看看几个核心的成员变量:
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16
/** * The maximum capacity, used if a higher value is implicitly specified * by either of the constructors with arguments. * MUST be a power of two <= 1<<30. */
static final int MAXIMUM_CAPACITY = 1 << 30;
/** * The load factor used when none specified in constructor. */
static final float DEFAULT_LOAD_FACTOR = 0.75f;
static final int TREEIFY_THRESHOLD = 8;
transient Node<K,V>[] table;
/** * Holds cached entrySet(). Note that AbstractMap fields are used * for keySet() and values(). */
transient Set<Map.Entry<K,V>> entrySet;
/** * The number of key-value mappings contained in this map. */
transient int size;
复制代码
和 1.7 大致上都差很少,仍是有几个重要的区别:
TREEIFY_THRESHOLD
用于判断是否须要将链表转换为红黑树的阈值。Node 的核心组成其实也是和 1.7 中的 HashEntry 同样,存放的都是 key value hashcode next
等数据。
再来看看核心方法。
看似要比 1.7 的复杂,咱们一步步拆解:
key、key 的 hashcode
与写入的 key 是否相等,相等就赋值给 e
,在第 8 步的时候会统一进行赋值及返回。e != null
就至关于存在相同的 key,那就须要将值覆盖。public V get(Object key) {
Node<K,V> e;
return (e = getNode(hash(key), key)) == null ? null : e.value;
}
final Node<K,V> getNode(int hash, Object key) {
Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
if ((tab = table) != null && (n = tab.length) > 0 &&
(first = tab[(n - 1) & hash]) != null) {
if (first.hash == hash && // always check first node
((k = first.key) == key || (key != null && key.equals(k))))
return first;
if ((e = first.next) != null) {
if (first instanceof TreeNode)
return ((TreeNode<K,V>)first).getTreeNode(hash, key);
do {
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
return e;
} while ((e = e.next) != null);
}
}
return null;
}
复制代码
get 方法看起来就要简单许多了。
从这两个核心方法(get/put)能够看出 1.8 中对大链表作了优化,修改成红黑树以后查询效率直接提升到了 O(logn)
。
可是 HashMap 原有的问题也都存在,好比在并发场景下使用时容易出现死循环。
final HashMap<String, String> map = new HashMap<String, String>();
for (int i = 0; i < 1000; i++) {
new Thread(new Runnable() {
@Override
public void run() {
map.put(UUID.randomUUID().toString(), "");
}
}).start();
}
复制代码
可是为何呢?简单分析下。
看过上文的还记得在 HashMap 扩容的时候会调用 resize()
方法,就是这里的并发操做容易在一个桶上造成环形链表;这样当获取一个不存在的 key 时,计算出的 index 正好是环形链表的下标就会出现死循环。
以下图:
还有一个值得注意的是 HashMap 的遍历方式,一般有如下几种:
Iterator<Map.Entry<String, Integer>> entryIterator = map.entrySet().iterator();
while (entryIterator.hasNext()) {
Map.Entry<String, Integer> next = entryIterator.next();
System.out.println("key=" + next.getKey() + " value=" + next.getValue());
}
Iterator<String> iterator = map.keySet().iterator();
while (iterator.hasNext()){
String key = iterator.next();
System.out.println("key=" + key + " value=" + map.get(key));
}
复制代码
强烈建议
使用第一种 EntrySet 进行遍历。
第一种能够把 key value 同时取出,第二种还得须要经过 key 取一次 value,效率较低。
简单总结下 HashMap:不管是 1.7 仍是 1.8 其实都能看出 JDK 没有对它作任何的同步操做,因此并发会出问题,甚至出现死循环致使系统不可用。
所以 JDK 推出了专项专用的 ConcurrentHashMap ,该类位于 java.util.concurrent
包下,专门用于解决并发问题。
坚持看到这里的朋友算是已经把 ConcurrentHashMap 的基础已经打牢了,下面正式开始分析。
ConcurrentHashMap 一样也分为 1.7 、1.8 版,二者在实现上略有不一样。
先来看看 1.7 的实现,下面是他的结构图:
如图所示,是由 Segment 数组、HashEntry 组成,和 HashMap 同样,仍然是数组加链表。
它的核心成员变量:
/** * Segment 数组,存放数据时首先须要定位到具体的 Segment 中。 */
final Segment<K,V>[] segments;
transient Set<K> keySet;
transient Set<Map.Entry<K,V>> entrySet;
复制代码
Segment 是 ConcurrentHashMap 的一个内部类,主要的组成以下:
static final class Segment<K,V> extends ReentrantLock implements Serializable {
private static final long serialVersionUID = 2249069246763182397L;
// 和 HashMap 中的 HashEntry 做用同样,真正存放数据的桶
transient volatile HashEntry<K,V>[] table;
transient int count;
transient int modCount;
transient int threshold;
final float loadFactor;
}
复制代码
看看其中 HashEntry 的组成:
和 HashMap 很是相似,惟一的区别就是其中的核心数据如 value ,以及链表都是 volatile 修饰的,保证了获取时的可见性。
原理上来讲:ConcurrentHashMap 采用了分段锁技术,其中 Segment 继承于 ReentrantLock。不会像 HashTable 那样不论是 put 仍是 get 操做都须要作同步处理,理论上 ConcurrentHashMap 支持 CurrencyLevel (Segment 数组数量)的线程并发。每当一个线程占用锁访问一个 Segment 时,不会影响到其余的 Segment。
下面也来看看核心的 put get
方法。
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);
}
复制代码
首先是经过 key 定位到 Segment,以后在对应的 Segment 中进行具体的 put。
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;
}
复制代码
虽然 HashEntry 中的 value 是用 volatile 关键词修饰的,可是并不能保证并发的原子性,因此 put 操做时仍然须要加锁处理。
首先第一步的时候会尝试获取锁,若是获取失败确定就有其余线程存在竞争,则利用 scanAndLockForPut()
自旋获取锁。
MAX_SCAN_RETRIES
则改成阻塞锁获取,保证能获取成功。再结合图看看 put 的流程。
public V get(Object key) {
Segment<K,V> s; // manually integrate access methods to reduce overhead
HashEntry<K,V>[] tab;
int h = hash(key);
long u = (((h >>> segmentShift) & segmentMask) << SSHIFT) + SBASE;
if ((s = (Segment<K,V>)UNSAFE.getObjectVolatile(segments, u)) != null &&
(tab = s.table) != null) {
for (HashEntry<K,V> e = (HashEntry<K,V>) UNSAFE.getObjectVolatile
(tab, ((long)(((tab.length - 1) & h)) << TSHIFT) + TBASE);
e != null; e = e.next) {
K k;
if ((k = e.key) == key || (e.hash == h && key.equals(k)))
return e.value;
}
}
return null;
}
复制代码
get 逻辑比较简单:
只须要将 Key 经过 Hash 以后定位到具体的 Segment ,再经过一次 Hash 定位到具体的元素上。
因为 HashEntry 中的 value 属性是用 volatile 关键词修饰的,保证了内存可见性,因此每次获取时都是最新值。
ConcurrentHashMap 的 get 方法是很是高效的,由于整个过程都不须要加锁。
1.7 已经解决了并发问题,而且能支持 N 个 Segment 这么屡次数的并发,但依然存在 HashMap 在 1.7 版本中的问题。
那就是查询遍历链表效率过低。
所以 1.8 作了一些数据结构上的调整。
首先来看下底层的组成结构:
看起来是否是和 1.8 HashMap 结构相似?
其中抛弃了原有的 Segment 分段锁,而采用了 CAS + synchronized
来保证并发安全性。
也将 1.7 中存放数据的 HashEntry 改成 Node,但做用都是相同的。
其中的 val next
都用了 volatile 修饰,保证了可见性。
重点来看看 put 函数:
f
即为当前 key 定位出的 Node,若是为空表示当前位置能够写入数据,利用 CAS 尝试写入,失败则自旋保证成功。hashcode == MOVED == -1
,则须要进行扩容。TREEIFY_THRESHOLD
则要转换为红黑树。1.8 在 1.7 的数据结构上作了大的改动,采用红黑树以后能够保证查询效率(
O(logn)
),甚至取消了 ReentrantLock 改成了 synchronized,这样能够看出在新版的 JDK 中对 synchronized 优化是很到位的。
看完了整个 HashMap 和 ConcurrentHashMap 在 1.7 和 1.8 中不一样的实现方式相信你们对他们的理解应该会更加到位。
其实这块也是面试的重点内容,一般的套路是:
这一串问题相信你们仔细看完都能怼回面试官。
除了面试会问到以外平时的应用其实也蛮多,像以前谈到的 Guava 中 Cache 的实现就是利用 ConcurrentHashMap 的思想。
同时也能学习 JDK 做者大牛们的优化思路以及并发解决方案。
其实写这篇的前提是源于 GitHub 上的一个 Issues,也但愿你们能参与进来,共同维护好这个项目。
最近在总结一些 Java 相关的知识点,感兴趣的朋友能够一块儿维护。
欢迎关注公众号一块儿交流: