机制:分段加锁
Segment的定义如下:
/**
* Segments are specialized versions of hash tables. This
* subclasses from ReentrantLock opportunistically, just to
* simplify some locking and avoid separate construction.
*/
static final class Segment extends ReentrantLock implements Serializable {
/*
* Segments maintain a table of entry lists that are always
* kept in a consistent state, so can be read (via volatile
* reads of segments and tables) without locking. This
* requires replicating nodes when necessary during table
* resizing, so the old lists can be traversed by readers
* still using old version of table.
*
* This class defines only mutative methods requiring locking.
* Except as noted, the methods of this class perform the
* per-segment versions of ConcurrentHashMap methods. (Other
* methods are integrated directly into ConcurrentHashMap
* methods.) These mutative methods use a form of controlled
* spinning on contention via methods scanAndLock and
* scanAndLockForPut. These intersperse tryLocks with
* traversals to locate nodes. The main benefit is to absorb
* cache misses (which are very common for hash tables) while
* obtaining locks so that traversal is faster once
* acquired. We do not actually use the found nodes since they
* must be re-acquired under lock anyway to ensure sequential
* consistency of updates (and in any case may be undetectably
* stale), but they will normally be much faster to re-locate.
* Also, scanAndLockForPut speculatively creates a fresh node
* to use in put if no node is found.
*/
private static final long serialVersionUID = 2249069246763182397L;
/**
* The maximum number of times to tryLock in a prescan before
* possibly blocking on acquire in preparation for a locked
* segment operation. On multiprocessors, using a bounded
* number of retries maintains cache acquired while locating
* nodes.
*/
static final int MAX_SCAN_RETRIES =
Runtime.getRuntime().availableProcessors() > 1 ? 64 : 1;
/**
* The per-segment table. Elements are accessed via
* entryAt/setEntryAt providing volatile semantics.
*/
transient volatile HashEntry[] table;
/**
* The number of elements. Accessed only either within locks
* or among other volatile reads that maintain visibility.
*/
transient int count;
/**
* The total number of mutative operations in this segment.
* Even though this may overflows 32 bits, it provides
* sufficient accuracy for stability checks in CHM isEmpty()
* and size() methods. Accessed only either within locks or
* among other volatile reads that maintain visibility.
*/
transient int modCount;
/**
* The table is rehashed when its size exceeds this threshold.
* (The value of this field is always (int)(capacity *
* loadFactor).)
*/
transient int threshold;
/**
* The load factor for the hash table. Even though this value
* is same for all segments, it is replicated to avoid needing
* links to outer object.
* @serial
*/
final float loadFactor;
Segment(float lf, int threshold, HashEntry[] tab) {
this.loadFactor = lf;
this.threshold = threshold;
this.table = tab;
}
final V put(K key, int hash, V value, boolean onlyIfAbsent) {
HashEntry node = tryLock() ? null :
scanAndLockForPut(key, hash, value);
V oldValue;
try {
HashEntry[] tab = table;
int index = (tab.length - 1) & hash;
HashEntry first = entryAt(tab, index);
for (HashEntry 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(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;
}
/**
* Doubles size of table and repacks entries, also adding the
* given node to new table
*/
@SuppressWarnings("unchecked")
private void rehash(HashEntry node) {
/*
* Reclassify nodes in each list to new table. Because we
* are using power-of-two expansion, the elements from
* each bin must either stay at same index, or move with a
* power of two offset. We eliminate unnecessary node
* creation by catching cases where old nodes can be
* reused because their next fields won't change.
* Statistically, at the default threshold, only about
* one-sixth of them need cloning when a table
* doubles. The nodes they replace will be garbage
* collectable as soon as they are no longer referenced by
* any reader thread that may be in the midst of
* concurrently traversing table. Entry accesses use plain
* array indexing because they are followed by volatile
* table write.
*/
HashEntry[] oldTable = table;
int oldCapacity = oldTable.length;
int newCapacity = oldCapacity << 1;
threshold = (int)(newCapacity * loadFactor);
HashEntry[] newTable =
(HashEntry[]) new HashEntry[newCapacity];
int sizeMask = newCapacity - 1;
for (int i = 0; i < oldCapacity ; i++) {
HashEntry e = oldTable[i];
if (e != null) {
HashEntry next = e.next;
int idx = e.hash & sizeMask;
if (next == null) // Single node on list
newTable[idx] = e;
else { // Reuse consecutive sequence at same slot
HashEntry lastRun = e;
int lastIdx = idx;
for (HashEntry last = next;
last != null;
last = last.next) {
int k = last.hash & sizeMask;
if (k != lastIdx) {
lastIdx = k;
lastRun = last;
}
}
newTable[lastIdx] = lastRun;
// Clone remaining nodes
for (HashEntry p = e; p != lastRun; p = p.next) {
V v = p.value;
int h = p.hash;
int k = h & sizeMask;
HashEntry n = newTable[k];
newTable[k] = new HashEntry(h, p.key, v, n);
}
}
}
}
int nodeIndex = node.hash & sizeMask; // add the new node
node.setNext(newTable[nodeIndex]);
newTable[nodeIndex] = node;
table = newTable;
}
/**
* Scans for a node containing given key while trying to
* acquire lock, creating and returning one if not found. Upon
* return, guarantees that lock is held. UNlike in most
* methods, calls to method equals are not screened: Since
* traversal speed doesn't matter, we might as well help warm
* up the associated code and accesses as well.
*
* @return a new node if key not found, else null
*/
private HashEntry scanAndLockForPut(K key, int hash, V value) {
HashEntry first = entryForHash(this, hash);
HashEntry e = first;
HashEntry node = null;
int retries = -1; // negative while locating node
while (!tryLock()) {
HashEntry f; // to recheck first below
if (retries < 0) {
if (e == null) {
if (node == null) // speculatively create node
node = new HashEntry(hash, key, value, null);
retries = 0;
}
else if (key.equals(e.key))
retries = 0;
else
e = e.next;
}
else if (++retries > MAX_SCAN_RETRIES) {
lock();
break;
}
else if ((retries & 1) == 0 &&
(f = entryForHash(this, hash)) != first) {
e = first = f; // re-traverse if entry changed
retries = -1;
}
}
return node;
}
/**
* Scans for a node containing the given key while trying to
* acquire lock for a remove or replace operation. Upon
* return, guarantees that lock is held. Note that we must
* lock even if the key is not found, to ensure sequential
* consistency of updates.
*/
private void scanAndLock(Object key, int hash) {
// similar to but simpler than scanAndLockForPut
HashEntry first = entryForHash(this, hash);
HashEntry e = first;
int retries = -1;
while (!tryLock()) {
HashEntry f;
if (retries < 0) {
if (e == null || key.equals(e.key))
retries = 0;
else
e = e.next;
}
else if (++retries > MAX_SCAN_RETRIES) {
lock();
break;
}
else if ((retries & 1) == 0 &&
(f = entryForHash(this, hash)) != first) {
e = first = f;
retries = -1;
}
}
}
/**
* Remove; match on key only if value null, else match both.
*/
final V remove(Object key, int hash, Object value) {
if (!tryLock())
scanAndLock(key, hash);
V oldValue = null;
try {
HashEntry[] tab = table;
int index = (tab.length - 1) & hash;
HashEntry e = entryAt(tab, index);
HashEntry pred = null;
while (e != null) {
K k;
HashEntry next = e.next;
if ((k = e.key) == key ||
(e.hash == hash && key.equals(k))) {
V v = e.value;
if (value == null || value == v || value.equals(v)) {
if (pred == null)
setEntryAt(tab, index, next);
else
pred.setNext(next);
++modCount;
--count;
oldValue = v;
}
break;
}
pred = e;
e = next;
}
} finally {
unlock();
}
return oldValue;
}
final boolean replace(K key, int hash, V oldValue, V newValue) {
if (!tryLock())
scanAndLock(key, hash);
boolean replaced = false;
try {
HashEntry e;
for (e = entryForHash(this, hash); e != null; e = e.next) {
K k;
if ((k = e.key) == key ||
(e.hash == hash && key.equals(k))) {
if (oldValue.equals(e.value)) {
e.value = newValue;
++modCount;
replaced = true;
}
break;
}
}
} finally {
unlock();
}
return replaced;
}
final V replace(K key, int hash, V value) {
if (!tryLock())
scanAndLock(key, hash);
V oldValue = null;
try {
HashEntry e;
for (e = entryForHash(this, hash); e != null; e = e.next) {
K k;
if ((k = e.key) == key ||
(e.hash == hash && key.equals(k))) {
oldValue = e.value;
e.value = value;
++modCount;
break;
}
}
} finally {
unlock();
}
return oldValue;
}
final void clear() {
lock();
try {
HashEntry[] tab = table;
for (int i = 0; i < tab.length ; i++)
setEntryAt(tab, i, null);
++modCount;
count = 0;
} finally {
unlock();
}
}
}
Segment内部持有HashMap的字段,比如长度、加载因子、阈值等等,并且其中方法包含put、remove、replace,所以可以猜测,ConcurrentHashMap将方法接口委托给了Segment,下面可以具体分析。Segment继承自ReentrantLock,拥有可重入锁的性质。
ConcurrentHashMap的构造方法主要需要对三个字段进行赋值,分别是容量、加载因子和并发参数,其中前两个参数好理解,后一个参数下面具体介绍。
public ConcurrentHashMap(int initialCapacity,
float loadFactor, int concurrencyLevel) {
if (!(loadFactor > 0) || initialCapacity < 0 || concurrencyLevel <= 0)
throw new IllegalArgumentException();
if (concurrencyLevel > MAX_SEGMENTS)
concurrencyLevel = MAX_SEGMENTS;
// ssize最终是2的指数倍数,如果并发因素为16,那么该ssize将会得到16
int sshift = 0;
int ssize = 1;
while (ssize < concurrencyLevel) {
++sshift;
ssize <<= 1;
}
this.segmentShift = 32 - sshift;
this.segmentMask = ssize - 1;
//判断初始容量
if (initialCapacity > MAXIMUM_CAPACITY)
initialCapacity = MAXIMUM_CAPACITY;
int c = initialCapacity / ssize;
if (c * ssize < initialCapacity)
++c;
int cap = MIN_SEGMENT_TABLE_CAPACITY;
while (cap < c)
cap <<= 1;
// create segments and segments[0]
Segment s0 =
new Segment(loadFactor, (int)(cap * loadFactor),
(HashEntry[])new HashEntry[cap]);
Segment[] ss = (Segment[])new Segment[ssize];
UNSAFE.putOrderedObject(ss, SBASE, s0); // ordered write of segments[0]
this.segments = ss;
}
public ConcurrentHashMap(int initialCapacity, float loadFactor) {
this(initialCapacity, loadFactor, DEFAULT_CONCURRENCY_LEVEL);
}
public ConcurrentHashMap(int initialCapacity) {
this(initialCapacity, DEFAULT_LOAD_FACTOR, DEFAULT_CONCURRENCY_LEVEL);
}
public ConcurrentHashMap() {
this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR, DEFAULT_CONCURRENCY_LEVEL);
}
public ConcurrentHashMap(Map extends K, ? extends V> m) {
this(Math.max((int) (m.size() / DEFAULT_LOAD_FACTOR) + 1,
DEFAULT_INITIAL_CAPACITY),
DEFAULT_LOAD_FACTOR, DEFAULT_CONCURRENCY_LEVEL);
putAll(m);
}
从上面可以看到,默认的ConcurrentHashMap的初始容量为16,加载因子为0.75,并发参数为16。其余的方法分别可以设置这几个值,最终都会调用第一个构造方法。
如果调用默认的构造方法,那么最终得到的segments将是一个尺寸为16的数组,并且第一个元素为s0,其第三个参数是一个尺寸为2的数组。
可以发现ConcurrentHashMap和1.8中区别,1.7中没有table数组这样的字段,只有segments这样的字段
ConcureentHashMap的put方法如下所示:
public V put(K key, V value) {
Segment s;
//不允许value为null
if (value == null)
throw new NullPointerException();
//计算hash值
int hash = hash(key);
int j = (hash >>> segmentShift) & segmentMask;
//如果Segment不存在,调用ensureSegment方法
if ((s = (Segment)UNSAFE.getObject // nonvolatile; recheck
(segments, (j << SSHIFT) + SBASE)) == null) // in ensureSegment
s = ensureSegment(j);
//调用Segment的put方法插入键值对
return s.put(key, hash, value, false);
}
从上面的put方法可以得出几点:
1. value值不允许为null
2. UNSAFE采取的是CAS算法实现的线程安全,一旦getObject为null了,即说明有并发了,那么将调用ensureSegment()使用自旋的方式获取Segment。
下面首先看一下,ensureSegment()方法是如何最终返回一个Segment的。该方法的实现如下:
private Segment ensureSegment(int k) {
final Segment[] ss = this.segments;
long u = (k << SSHIFT) + SBASE; // raw offset
Segment seg;
//如果该索引处还未存在Segment,那么将创建一个Segment
if ((seg = (Segment)UNSAFE.getObjectVolatile(ss, u)) == null) {
//在构造方法中,初始化了一个s0对象作为原型
Segment proto = ss[0]; // use segment 0 as prototype
int cap = proto.table.length;
float lf = proto.loadFactor;
int threshold = (int)(cap * lf);
HashEntry[] tab = (HashEntry[])new HashEntry[cap];
//再次检查
if ((seg = (Segment)UNSAFE.getObjectVolatile(ss, u))
== null) { // recheck
//新建一个Segment,参数可以发现与s0相同
Segment s = new Segment(lf, threshold, tab);
//自旋插入,一旦插入,则跳出循环
while ((seg = (Segment)UNSAFE.getObjectVolatile(ss, u))
== null) {
if (UNSAFE.compareAndSwapObject(ss, u, null, seg = s))
break;
}
}
}
return seg;
}
从ensureSegment()方法可以看到,根据索引去Segments中取Segment,如果还没有创建Segment,那么将执行新建,然后自旋插入;而如果存在Segment,那么将自旋获取该Segment。
其中创建的Segment与s0相同,而s0最初的状态是在构造方法中指定的。
当Segment创建好后,再看Segment的put方法,其实现如下:
final V put(K key, int hash, V value, boolean onlyIfAbsent) {
//尝试获取锁,如果获取到锁,那么node为null,否则调用scanAndLockForPut方法
HashEntry node = tryLock() ? null :
scanAndLockForPut(key, hash, value);
V oldValue;
try {
HashEntry[] tab = table;
//计算位于本Segment中的Table中的索引
int index = (tab.length - 1) & hash;
//得到链表首节点
HashEntry first = entryAt(tab, index);
//遍历
for (HashEntry 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 {
//如果node不为null,重用node,指向链表头节点
if (node != null)
node.setNext(first);
//如果为null,那么新建一个节点
else
node = new HashEntry(hash, key, value, first);
int c = count + 1;
//如果超过了阈值并且表格长度小于最大容量,那么执行rehash操作
if (c > threshold && tab.length < MAXIMUM_CAPACITY)
rehash(node);
//否则,将node节点更新到table中
else
setEntryAt(tab, index, node);
++modCount;
count = c;
oldValue = null;
break;
}
}
} finally {
//释放锁
unlock();
}
return oldValue;
}
从上面可以看到Segment的put操作的流程:
1. 调用tryLock()方法获取锁,一旦获取到锁后,node为null,那么执行下面的插入操作;
2. 如果tryLock()方法获取失败,即目前有线程正在持有该Segment,那么调用scanAndLockForPut()方法;
3. 插入过程中,需要遍历链表,如果是新节点,则会作为链表的头节点
4. 插入一个节点后,如果需要进行rehash操作,则会调用rehash()方法,否则就是将链表更新到表中
5. 最后释放锁
下面看一下scanAndLockForPut()方法是如何在Segment被其他线程使用时扫描获取到锁的,其实现如下:
private HashEntry scanAndLockForPut(K key, int hash, V value) {
//得到待插入桶的头节点
HashEntry first = entryForHash(this, hash);
HashEntry e = first;
HashEntry node = null;
int retries = -1; // negative while locating node
//不断尝试tryLock()方法
while (!tryLock()) {
//如果失败
HashEntry f; // to recheck first below
if (retries < 0) {
if (e == null) {
if (node == null) // speculatively create node
node = new HashEntry(hash, key, value, null);
retries = 0;
}
else if (key.equals(e.key))
retries = 0;
else
e = e.next;
}
//如果重试次数很多后,那么调用lock()方法加入到ReetrantLock的等待队列中,跳出循环
else if (++retries > MAX_SCAN_RETRIES) {
lock();
break;
}
else if ((retries & 1) == 0 &&
(f = entryForHash(this, hash)) != first) {
e = first = f; // re-traverse if entry changed
retries = -1;
}
}
return node;
}
从scanAndLockForPut()方法主要完成扫描和获取锁,一旦该方法返回,表明已经获取到锁了。
下面看一下rehash方法,看一个Segment中是如何进行rehash操作的,其实现如下:
private void rehash(HashEntry node) {
HashEntry[] oldTable = table;
int oldCapacity = oldTable.length;
//新容量为旧容量的2倍
int newCapacity = oldCapacity << 1;
//新的阈值为新容量*加载因子
threshold = (int)(newCapacity * loadFactor);
//创建新表
HashEntry[] newTable =
(HashEntry[]) new HashEntry[newCapacity];
int sizeMask = newCapacity - 1;
//对旧表做遍历
for (int i = 0; i < oldCapacity ; i++) {
//得到桶中元素
HashEntry e = oldTable[i];
//如果该桶中存在元素,需要做转移操作
if (e != null) {
//头节点的下一个节点
HashEntry next = e.next;
//得到在新表中的位置
int idx = e.hash & sizeMask;
//链表中只存在一个节点,将链表头节点赋值到新表中即可
if (next == null) // Single node on list
newTable[idx] = e;
//存在后续节点
else { // 重用在一个桶中连续的序列
HashEntry lastRun = e;
int lastIdx = idx;
for (HashEntry last = next;
last != null;
last = last.next) {
int k = last.hash & sizeMask;
if (k != lastIdx) {
lastIdx = k;
lastRun = last;
}
}
newTable[lastIdx] = lastRun;
// 克隆剩余节点
for (HashEntry p = e; p != lastRun; p = p.next) {
V v = p.value;
int h = p.hash;
int k = h & sizeMask;
HashEntry n = newTable[k];
newTable[k] = new HashEntry(h, p.key, v, n);
}
}
}
}
int nodeIndex = node.hash & sizeMask; // add the new node
node.setNext(newTable[nodeIndex]);
newTable[nodeIndex] = node;
//更改新表指向
table = newTable;
}
从上面可以看到,rehash时容量会扩大一倍。在对旧元素重新hash获取桶的位置时,不太明白为什么要做两次遍历,区分出连续的序列。完全可以使用另外的方法进行区分,比如1.8中的分配方法。
看完了ConcurrentHashMap的put方法后,可以再看一下get方法是如何实现的,get方法是不加锁的,其实现如下:
public V get(Object key) {
Segment s; // manually integrate access methods to reduce overhead
HashEntry[] tab;
int h = hash(key);
long u = (((h >>> segmentShift) & segmentMask) << SSHIFT) + SBASE;
//根据位置取Segment的索引
if ((s = (Segment)UNSAFE.getObjectVolatile(segments, u)) != null &&
(tab = s.table) != null) {
//遍历
for (HashEntry e = (HashEntry) 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;
}
ConcurrentHashMap的get方法是不加锁的,所以只要Segment不为null,那么就做一个遍历即可。
由于ConcureentHashMap中管理Segment,而Segment又管理HashEntry数组,所以ConcurrentHashMap的size()方法应该是累加每一个Segment中的元素个数,其实现如下:
public int size() {
//复制一份拷贝
final Segment[] segments = this.segments;
int size;
boolean overflow; // true if size overflows 32 bits
long sum; // sum of modCounts
long last = 0L; // previous sum
int retries = -1; // first iteration isn't retry
try {
//死循环
for (;;) {
if (retries++ == RETRIES_BEFORE_LOCK) {
for (int j = 0; j < segments.length; ++j)
ensureSegment(j).lock(); // force creation
}
sum = 0L;
size = 0;
overflow = false;
//遍历Segments
for (int j = 0; j < segments.length; ++j) {
Segment seg = segmentAt(segments, j);
if (seg != null) {
sum += seg.modCount;
int c = seg.count;
if (c < 0 || (size += c) < 0)
overflow = true;
}
}
//如果两次值一样,那么认为该值一样,返回
if (sum == last)
break;
last = sum;
}
} finally {
//如果之前加锁了,那么需要对每一个Segment释放锁
if (retries > RETRIES_BEFORE_LOCK) {
for (int j = 0; j < segments.length; ++j)
segmentAt(segments, j).unlock();
}
}
//如果size超过了Integer.MAX_VALUE,那么将返回Integer.MAX_VALUE
return overflow ? Integer.MAX_VALUE : size;
}
从上面可以看到size()方法如果在retries为0和1时两次计算的sum值一样,那么将会跳出循环返回该值;而如果两次该值不相同,那么就会尝试锁住每一个Segment,然后再累加每一个segment中的数量。
最后在返回值的时候需要注意,如果值超过了Integer.MAX_VALUE,那么只会Integer.MAX_VALUE。
那么为什么会超过Integer.MAX_VALUE值呢?
这是因为每一个Segment中的最大元素个数为MAXIMUM_CAPACITY(2^30),而ConcurrentHashMap最多有MAX_SEGMENTS(2^16)个Segment,那么一个ConcurrentHashMap最多将会有(2^46)个元素,自然是可能超过int的最大值的。
JDK1.7中ConcurrentHashMap采用的是借助于Segment的分段加锁机制+CAS实现的线程安全,每一个Segment负责管理其内部的Table,每一个Segment其实类似于一个HashMap,其内部是线程安全的,因为其线程安全是外部Segment所提供的。