环境:JDK 8
概述
- 支持检索的完全并发和更新的高预期并发性。
- 该类遵循相同的功能规范Hashtable,并包括与每种方法相对应的方法版本 Hashtable;即功能和Hashtable基本相同。
- 所有操作都是线程安全的;检索操作也不需要锁定,并发操作中不会锁定整张表,此类与Hashtable依赖于其线程安全但不依赖于其同步细节的程序完全可互操作。
构造函数摘要
Constructor | description |
---|---|
ConcurrentHashMap() | 使用默认的初始表大小(16)创建一个新的空映射。 |
ConcurrentHashMap(int initialCapacity) | 创建一个新的空映射,其初始表大小容纳指定数量的元素,而无需动态调整大小。 |
ConcurrentHashMap(int initialCapacity, float loadFactor) | 根据给定的元素数(initialCapacity)和初始表密度(loadFactor)创建一个新的空映射,其初始表大小。 |
ConcurrentHashMap(int initialCapacity, float loadFactor, int concurrencyLevel) | 创建一个新的空映射,其初始表大小基于给定的元素数(initialCapacity),表密度(loadFactor)和并发更新线程数(concurrencyLevel)。 |
ConcurrentHashMap(Map<? extends K,? extends V> m) | 创建一个与给定地图具有相同映射的新地图。 |
方法摘要
put()
public V put(K key, V value) {
return putVal(key, value, false);
}
/** Implementation for put and putIfAbsent */
final V putVal(K key, V value, boolean onlyIfAbsent) {
// key 和 value 都不能为null
if (key == null || value == null) throw new NullPointerException();
int hash = spread(key.hashCode());
int binCount = 0;
for (Node<K,V>[] tab = table;;) {
Node<K,V> f; int n, i, fh;
// 数组为空
if (tab == null || (n = tab.length) == 0)
// 进行初始化
tab = initTable();
// node 不存在,同时赋值数组下标i=(n - 1) & hash
else if ((f = tabAt(tab, i = (n - 1) & hash)) == null) {
// 使用CAS机制添加新node
if (casTabAt(tab, i, null,
new Node<K,V>(hash, key, value, null)))
break; // no lock when adding to empty bin
}
// Map正在扩容
else if ((fh = f.hash) == MOVED)
// tab指向扩容后的数组
tab = helpTransfer(tab, f);
else {
V oldVal = null;
// 锁定tab[i]
synchronized (f) {
// i=(n - 1) & hash
if (tabAt(tab, i) == f) {
if (fh >= 0) {
binCount = 1;
for (Node<K,V> e = f;; ++binCount) {
K ek;
if (e.hash == hash &&
((ek = e.key) == key ||
(ek != null && key.equals(ek)))) {
oldVal = e.val;
// 已存在,更新value
if (!onlyIfAbsent)
e.val = value;
break;
}
Node<K,V> pred = e;
// 在链表末增加新node
if ((e = e.next) == null) {
pred.next = new Node<K,V>(hash, key,
value, null);
break;
}
}
}
// 是否为红黑树结构
else if (f instanceof TreeBin) {
Node<K,V> p;
binCount = 2;
if ((p = ((TreeBin<K,V>)f).putTreeVal(hash, key,
value)) != null) {
oldVal = p.val;
// 已存在,更新value
if (!onlyIfAbsent)
p.val = value;
}
}
}
}
if (binCount != 0) {
// 达到阈值,红黑树化
if (binCount >= TREEIFY_THRESHOLD)
treeifyBin(tab, i);
if (oldVal != null)
return oldVal;
break;
}
}
}
// 记录操作
addCount(1L, binCount);
return null;
}
remove
/**
* Removes the key (and its corresponding value) from this map.
* This method does nothing if the key is not in the map.
*
* @param key the key that needs to be removed
* @return the previous value associated with {@code key}, or
* {@code null} if there was no mapping for {@code key}
* @throws NullPointerException if the specified key is null
*/
public V remove(Object key) {
return replaceNode(key, null, null);
}
/**
* Implementation for the four public remove/replace methods:
* Replaces node value with v, conditional upon match of cv if
* non-null. If resulting value is null, delete.
*/
final V replaceNode(Object key, V value, Object cv) {
// 计算hash值
int hash = spread(key.hashCode());
for (Node<K,V>[] tab = table;;) {
Node<K,V> f; int n, i, fh;
// 如果数组为空,或者tab[i]为空,都不操作
if (tab == null || (n = tab.length) == 0 ||
(f = tabAt(tab, i = (n - 1) & hash)) == null) // 赋值i = (n - 1) & hash后用
break;
// 数组正在扩容
else if ((fh = f.hash) == MOVED)
tab = helpTransfer(tab, f);
else {
V oldVal = null;
boolean validated = false;
// 锁定tab[i]
synchronized (f) {
if (tabAt(tab, i) == f) {
if (fh >= 0) {
validated = true;
for (Node<K,V> e = f, pred = null;;) {
K ek;
if (e.hash == hash &&
((ek = e.key) == key ||
(ek != null && key.equals(ek)))) {
V ev = e.val;
if (cv == null || cv == ev ||
(ev != null && cv.equals(ev))) {
oldVal = ev;
if (value != null)
e.val = value;
else if (pred != null)
pred.next = e.next; // e is target node,前一个node指向e的后一个节点,完成删除操作
else
setTabAt(tab, i, e.next); //e为第一个节点,要将tab[i]第一个节点设为e.next;
}
break;
}
pred = e;
// 遍历
if ((e = e.next) == null)
break;
}
}
// 红黑树结构
else if (f instanceof TreeBin) {
validated = true;
TreeBin<K,V> t = (TreeBin<K,V>)f;
TreeNode<K,V> r, p;
if ((r = t.root) != null &&
(p = r.findTreeNode(hash, key, null)) != null) {
V pv = p.val;
if (cv == null || cv == pv ||
(pv != null && cv.equals(pv))) {
oldVal = pv;
if (value != null)
p.val = value;
else if (t.removeTreeNode(p))
setTabAt(tab, i, untreeify(t.first));
}
}
}
}
}
if (validated) {
if (oldVal != null) {
if (value == null)
// 元素个数-1
addCount(-1L, -1);
return oldVal;
}
break;
}
}
}
return null;
}