JDK1.6/JDK1.7 ConcurrentHashMap是HashMap的线程安全实现,相比于Hashtable每次锁住整张表的情况,concurrentHashMap使用了分段锁(降低锁粒度),每次只锁住一个结点。 JDK1.8中已经摒弃了分段所,采取CAS替代了分段锁实现了无锁操作。
和HashMap的内部结点一致,定义了Node结点。
static class Node<K,V> implements Map.Entry<K,V> {
final int hash;
final K key;
volatile V val;
volatile Node<K,V> next;
Node(int hash, K key, V val, Node<K,V> next) {
this.hash = hash;
this.key = key;
this.val = val;
this.next = next;
}
public final K getKey() { return key; }
public final V getValue() { return val; }
public final int hashCode() { return key.hashCode() ^ val.hashCode(); }
public final String toString(){ return key + "=" + val; }
public final V setValue(V value) {
throw new UnsupportedOperationException();
}
//比较两个节点是否相同
public final boolean equals(Object o) {
Object k, v, u; Map.Entry<?,?> e;
return ((o instanceof Map.Entry) &&
(k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
(v = e.getValue()) != null &&
(k == key || k.equals(key)) &&
(v == (u = val) || v.equals(u)));
}
/**
* Virtualized support for map.get(); overridden in subclasses.
*/
//从链表中查找是否有哈希值和k均相同的结点
Node<K,V> find(int h, Object k) {
Node<K,V> e = this;
if (k != null) {
do {
K ek;
if (e.hash == h &&
((ek = e.key) == k || (ek != null && k.equals(ek))))
return e;
} while ((e = e.next) != null);
}
return null;
}
}
- put() 调用了putVal方法
public V put(K key, V value) {
return putVal(key, value, false);
}
- putVal()
final V putVal(K key, V value, boolean onlyIfAbsent) {
if (key == null || value == null) throw new NullPointerException();
int hash = spread(key.hashCode()); //通过spread方法计算出hash值。
int binCount = 0;
for (Node<K,V>[] tab = table;;) { //不断循环,和CAS配套使用
Node<K,V> f; int n, i, fh;
if (tab == null || (n = tab.length) == 0)
tab = initTable(); //第一次插入数据,创建新表。(参见initTable解析)
//根据hash原子性的获取表中对应的Node结点
else if ((f = tabAt(tab, i = (n - 1) & hash)) == null) {
//此时该结点为空,CAS,新建结点并存入表中
if (casTabAt(tab, i, null,
new Node<K,V>(hash, key, value, null)))
break; // no lock when adding to empty bin
}
else if ((fh = f.hash) == MOVED) //当前结点不为空,且rehash正在进行
//如果要扩容的新表存在,则将新值存入新表,并返回新表。
tab = helpTransfer(tab, f);
else {
V oldVal = null;
//给要修改的结点上锁
synchronized (f) {
//当前数据还没有被修改过,未出现脏读。不然进入下一次循环,重新进行。
if (tabAt(tab, i) == f) {
//此处插入的代码和HashMap一致
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;
if (!onlyIfAbsent)
e.val = value;
break;
}
Node<K,V> pred = e;
if ((e = e.next) == null) {//如果遍历到链表结尾仍没有找到,则添加新的对象。
pred.next = new Node<K,V>(hash, key,
value, null);
break;
}
}
}
else if (f instanceof TreeBin) {//如果冲突大于8,则转成树形结构,此时插入或更新结点至红黑树。
Node<K,V> p;
binCount = 2;
if ((p = ((TreeBin<K,V>)f).putTreeVal(hash, key,
value)) != null) {
oldVal = p.val;
if (!onlyIfAbsent)
p.val = value;
}
}
}
}
if (binCount != 0) {
if (binCount >= TREEIFY_THRESHOLD) //如果在插入元素后造成了链表长度大于8,则树化链表
treeifyBin(tab, i);
if (oldVal != null)
return oldVal;
break;
}
}
}
addCount(1L, binCount);//check and rehash
return null;
}
- initTable()
/**
* Initializes table, using the size recorded in sizeCtl.
*/
private final Node<K,V>[] initTable() {
Node<K,V>[] tab; int sc;
while ((tab = table) == null || tab.length == 0) {
//当sizeCtl小于0,说明表正在创建或rehash,放弃当前线程的控制权
if ((sc = sizeCtl) < 0)
Thread.yield(); // lost initialization race; just spin
//其中第一个参数为需要改变的对象,第二个为偏移量(即之前求出来的valueOffset的值),第三个参数为期待的值,第四个为更新后的值。->将sizeCtl设置为-1,开始创建表。
else if (U.compareAndSwapInt(this, SIZECTL, sc, -1)) {
try {
if ((tab = table) == null || tab.length == 0) {
//理论上sc的值应该为-1,但是由于多线程操作,可能当前值被别的线程修改,如果更新后的值是大于0,则选用sc的值作为哈希表的大小。
int n = (sc > 0) ? sc : DEFAULT_CAPACITY;
@SuppressWarnings("unchecked")
Node<K,V>[] nt = (Node<K,V>[])new Node<?,?>[n];
table = tab = nt;
sc = n - (n >>> 2); //sc = 0.75 * sc
}
} finally {
sizeCtl = sc;
}
break;
}
}
return tab;
}
- transfer() 扩容
//tab为当前的表地址,nextTab为扩容后的表
private final void transfer(Node<K,V>[] tab, Node<K,V>[] nextTab) {
int n = tab.length, stride;
//NCPU可以使用的CPU的数量,通过哈希表中的Node总量和CPU的数量来确定步长,即每个线程所允许处理的bucket的数量。
if ((stride = (NCPU > 1) ? (n >>> 3) / NCPU : n) < MIN_TRANSFER_STRIDE)
stride = MIN_TRANSFER_STRIDE; // subdivide range
if (nextTab == null) { // 新表为空,则创建一个新表
try {
@SuppressWarnings("unchecked")
Node<K,V>[] nt = (Node<K,V>[])new Node<?,?>[n << 1];//扩容至两倍
nextTab = nt;
} catch (Throwable ex) { // try to cope with OOME
sizeCtl = Integer.MAX_VALUE;
return;
}
nextTable = nextTab;
transferIndex = n;//rehash过程中正在转移的结点的指针,转移是从尾部向前开始的。
}
int nextn = nextTab.length;
ForwardingNode<K,V> fwd = new ForwardingNode<K,V>(nextTab);
boolean advance = true;
boolean finishing = false; // to ensure sweep before committing nextTab
for (int i = 0, bound = 0;;) {
Node<K,V> f; int fh;
while (advance) {
int nextIndex, nextBound;
if (--i >= bound || finishing)
advance = false;
// transferIndex = 0表示table中所有数组元素都已经有其他线程负责扩容
else if ((nextIndex = transferIndex) <= 0) {
i = -1;
advance = false;
}
//更新transferIndex的值
//更新成功,则当前线程负责完成索引为(nextBound,nextIndex)之间的桶首节点扩容
else if (U.compareAndSwapInt
(this, TRANSFERINDEX, nextIndex,
nextBound = (nextIndex > stride ?
nextIndex - stride : 0))) {
bound = nextBound;
i = nextIndex - 1;
advance = false;
}
}
if (i < 0 || i >= n || i + n >= nextn) {
int sc;
if (finishing) {
nextTable = null;
table = nextTab;
sizeCtl = (n << 1) - (n >>> 1); //设置load factor为0.75
return;
}
if (U.compareAndSwapInt(this, SIZECTL, sc = sizeCtl, sc - 1)) {
if ((sc - 2) != resizeStamp(n) << RESIZE_STAMP_SHIFT)
return;
finishing = advance = true;
i = n; // recheck before commit
}
}
else if ((f = tabAt(tab, i)) == null) //如果当前结点为空,添加一个转移结点,被再次被遍历到的时候将会读到hash值为MOVED
advance = casTabAt(tab, i, null, fwd);
else if ((fh = f.hash) == MOVED) //遍历到了一个已经被转移了的结点
advance = true; // already processed
else { //添加分段锁机制,锁住bucket,开始扩容
synchronized (f) {
if (tabAt(tab, i) == f) {//保证了在上锁之前值未被更改(宏观的原子性保证)
Node<K,V> ln, hn;
if (fh >= 0) {//拷贝链表的值
int runBit = fh & n;
Node<K,V> lastRun = f;
for (Node<K,V> p = f.next; p != null; p = p.next) {
int b = p.hash & n;
if (b != runBit) {
runBit = b;
lastRun = p;
}
}
if (runBit == 0) {
ln = lastRun;
hn = null;
}
else {
hn = lastRun;
ln = null;
}
for (Node<K,V> p = f; p != lastRun; p = p.next) {
int ph = p.hash; K pk = p.key; V pv = p.val;
if ((ph & n) == 0)
ln = new Node<K,V>(ph, pk, pv, ln);
else
hn = new Node<K,V>(ph, pk, pv, hn);
}
setTabAt(nextTab, i, ln);
setTabAt(nextTab, i + n, hn);
setTabAt(tab, i, fwd);
advance = true;
}
else if (f instanceof TreeBin) {//拷贝树
TreeBin<K,V> t = (TreeBin<K,V>)f;
TreeNode<K,V> lo = null, loTail = null;
TreeNode<K,V> hi = null, hiTail = null;
int lc = 0, hc = 0;
for (Node<K,V> e = t.first; e != null; e = e.next) {
int h = e.hash;
TreeNode<K,V> p = new TreeNode<K,V>
(h, e.key, e.val, null, null);
if ((h & n) == 0) {
if ((p.prev = loTail) == null)
lo = p;
else
loTail.next = p;
loTail = p;
++lc;
}
else {
if ((p.prev = hiTail) == null)
hi = p;
else
hiTail.next = p;
hiTail = p;
++hc;
}
}
ln = (lc <= UNTREEIFY_THRESHOLD) ? untreeify(lo) :
(hc != 0) ? new TreeBin<K,V>(lo) : t;
hn = (hc <= UNTREEIFY_THRESHOLD) ? untreeify(hi) :
(lc != 0) ? new TreeBin<K,V>(hi) : t;
setTabAt(nextTab, i, ln);
setTabAt(nextTab, i + n, hn);
setTabAt(tab, i, fwd);
advance = true;
}
}
}
}
}
}
- get() 哈希表的查询是O(1)级别的(没有哈希冲突的情况下)
public V get(Object key) {
Node<K,V>[] tab; Node<K,V> e, p; int n, eh; K ek;
int h = spread(key.hashCode());
if ((tab = table) != null && (n = tab.length) > 0 &&
(e = tabAt(tab, (n - 1) & h)) != null) {//通过CAS读取值,原子性
if ((eh = e.hash) == h) {//当前bucket是单一结点,直接返回。
if ((ek = e.key) == key || (ek != null && key.equals(ek)))
return e.val;
}
else if (eh < 0)//说明当前元素是树形结构
return (p = e.find(h, key)) != null ? p.val : null;
while ((e = e.next) != null) {//当前bucket是链表元素,通过遍历得到结果
if (e.hash == h &&
((ek = e.key) == key || (ek != null && key.equals(ek))))
return e.val;
}
}
return null;
}
继承自ReentrantLock Hashtable之所以慢是因为在进行CRUD时通过synchronized锁住了整张表,我们要优化时应减小锁粒度,这样每次只对最小的范围进行上锁,允许了高并发的实现。
Segment<K,V> s0 = new Segment<K,V>(loadFactor, (int)(cap * loadFactor),(HashEntry<K,V>[])new HashEntry[cap]);
没有Java8中的Node结点,取代的是继承自ReentrantLock的分段锁。
final Segment<K,V> segmentFor(int hash) { //定位锁
return segments[(hash >>> segmentShift) & segmentMask];
}
- get() 它的get方法里将要使用的共享变量都定义成volatile.
- put() get()和put()方法相较JAVA8简单很多,均是获得Segment后上锁进行操作。
1.ConcurrentHashMap源码解读(put/transfer/get)-jdk8 2.JDK7下ConcurrentHashMap源码分析