ConcurrentHashMap爲什麼高效?
與Hashtable不同的是,ConcurrentHashMap使用的是分段鎖技術,將ConcurrentHashMap容器的數據分段存儲,每一段數據分配一個Segment,當線程佔用一個Segment時,其他線程可以訪問其他段的數據.
概念
Segment : 可重入鎖,繼承ReentrantLock
HashEntry : 主要存儲鍵值對,可以叫節點
HashEntry結構:
static final class HashEntry<K,V> {
final int hash;
// key值初始化後不能改變
final K key;
//volatile保證讀到的數據爲最新值
volatile V value;
//volatile保證讀到的數據爲最新的
volatile HashEntry<K,V> next;
總結:
ConcurrentHashMap包含一個Segment數組,每個Segment包含一個HashEntry數組,當修改HashEntry數組採用開鏈法處理衝突,所以它的每個HashEntry元素又是鏈表結構的元素。
基本操作源碼分析
內部類
HashEntry
//HashEntry類,作爲一個Segment中的節點類。HashEntry類基本不可變。
static final class HashEntry<K,V> {
final int hash; //hash和key都是final,保證了讀操作時不用加鎖
final K key;
volatile V value;//爲了確保讀操作能夠看到最新的值,將value設置成volatile
volatile HashEntry<K,V> next;
//不再用final關鍵字,採用unsafe操作保證併發安全
HashEntry(int hash, K key, V value, HashEntry<K,V> next) {
this.hash = hash;
this.key = key;
this.value = value;
this.next = next;
}
//setNext方法可以設置該節點的next節點
final void setNext(HashEntry<K,V> n) {
UNSAFE.putOrderedObject(this, nextOffset, n);
}
// Unsafe mechanics
static final sun.misc.Unsafe UNSAFE;
static final long nextOffset;
static {
try {
UNSAFE = sun.misc.Unsafe.getUnsafe();
Class k = HashEntry.class;
nextOffset = UNSAFE.objectFieldOffset
(k.getDeclaredField("next"));
} catch (Exception e) {
throw new Error(e);
}
}
}
Setment
//Segment類
static final class Segment<K,V> extends ReentrantLock implements Serializable
//繼承ReentrantLock,說明每一個Segment都是一個鎖
Segment(float lf, int threshold, HashEntry<K,V>[] tab) {
this.loadFactor = lf;
this.threshold = threshold;
//HashEntry的數組
this.table = tab;
}
// 1.put方法,將一個HashEntry放入到該Segment中,使用自旋機制,減少了加鎖的可能性
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; //同hashMap相同的哈希定位方式
HashEntry<K,V> first = entryAt(tab, index);
for (HashEntry<K,V> e = first;;) {
if (e != null) {
//若不爲null,則持續查找,知道找到key和hash值相同的節點,將其value更新
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 { //若頭結點爲null
if (node != null) //在遍歷key對應節點鏈時沒有找到相應的節點
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); //如果超過閾值,則進行rehash操作
else
setEntryAt(tab, index, node);
++modCount;
count = c;
//沒有值,返回null
oldValue = null;
break;
}
}
} finally {
unlock();
}
return oldValue;
}
// 2.scanAndLockForPut方法,該操作持續查找key對應的節點鏈中是否已存在該節點,如果沒有找到已存在的節點,則預創建一個新節點,並且嘗試n次,直到嘗試次數超出限制,才真正進入等待狀態,即所謂的自旋等待。
private HashEntry<K,V> scanAndLockForPut(K key, int hash, V value) {
//根據hash值找到segment中的HashEntry節點
HashEntry<K,V> first = entryForHash(this, hash); //首先獲取頭結點
HashEntry<K,V> e = first;
HashEntry<K,V> node = null;
int retries = -1; // negative while locating node
while (!tryLock()) { //持續遍歷該哈希鏈
HashEntry<K,V> f; // to recheck first below
if (retries < 0) {
if (e == null) {
if (node == null) //若不存在要插入的節點,則創建一個新的節點
node = new HashEntry<K,V>(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;
}
/*當在自旋過程中發現節點鏈的鏈頭髮生了變化,則更新節點鏈的鏈頭,
並重置retries值爲-1,重新爲嘗試獲取鎖而自旋遍歷*/
else if ((retries & 1) == 0 &&
(f = entryForHash(this, hash)) != first) {
e = first = f; // re-traverse if entry changed
retries = -1;
}
}
return node;
}
// rehash方法,用於當容量超出閾值後,進行擴容操作,類似於hashMap的擴容操作
private void rehash(HashEntry<K,V> node) {
HashEntry<K,V>[] oldTable = table;
int oldCapacity = oldTable.length;
int newCapacity = oldCapacity << 1;
threshold = (int)(newCapacity * loadFactor);
HashEntry<K,V>[] newTable =
(HashEntry<K,V>[]) new HashEntry[newCapacity];
int sizeMask = newCapacity - 1;
for (int i = 0; i < oldCapacity ; i++) {
HashEntry<K,V> e = oldTable[i];
if (e != null) {
HashEntry<K,V> 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<K,V> lastRun = e;
int lastIdx = idx;
for (HashEntry<K,V> 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<K,V> p = e; p != lastRun; p = p.next) {
V v = p.value;
int h = p.hash;
int k = h & sizeMask;
HashEntry<K,V> n = newTable[k];
newTable[k] = new HashEntry<K,V>(h, p.key, v, n);
}
}
}
}
int nodeIndex = node.hash & sizeMask; // add the new node
node.setNext(newTable[nodeIndex]);
newTable[nodeIndex] = node;
table = newTable;
}
// remove方法,用於移除某個節點,返回移除的節點值
final V remove(Object key, int hash, Object value) {
if (!tryLock())
scanAndLock(key, hash);
V oldValue = null;
try {
HashEntry<K,V>[] tab = table;
int index = (tab.length - 1) & hash;
//根據這種哈希定位方式來定位對應的HashEntry
HashEntry<K,V> e = entryAt(tab, index);
HashEntry<K,V> pred = null;
while (e != null) {
K k;
HashEntry<K,V> 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;
}
// clear方法,要首先對整個segment加鎖,然後將每一個HashEntry都設置爲null
final void clear() {
lock();
try {
HashEntry<K,V>[] tab = table;
for (int i = 0; i < tab.length ; i++)
setEntryAt(tab, i, null);
++modCount;
count = 0;
} finally {
unlock();
}
}
構造方法
public ConcurrentHashMap(int initialCapacity,
float loadFactor, int concurrencyLevel) {
//處理異常情況
if (!(loadFactor > 0) || initialCapacity < 0 || concurrencyLevel <= 0)
throw new IllegalArgumentException();
//判斷併發級別是否大於最大併發級別(最大的併發等級不能超過MAX_SEGMENTS 1<<16(也就是1的二進制向左移16位,65536))
if (concurrencyLevel > MAX_SEGMENTS)
concurrencyLevel = MAX_SEGMENTS;
int sshift = 0;
int ssize = 1;
//取得大於數值最小的2的整數倍值
while (ssize < concurrencyLevel) {
++sshift;
ssize <<= 1;
}
//向左移動的位數
this.segmentShift = 32 - sshift; //3定位segment
//達到最後取餘的情況下(其餘爲正好全爲11),正好是&的結果
this.segmentMask = ssize - 1; //4定位segment
if (initialCapacity > MAXIMUM_CAPACITY)
initialCapacity = MAXIMUM_CAPACITY;
//c代表平均每個元素的多少(不足時,全+1)
int c = initialCapacity / ssize;
if (c * ssize < initialCapacity)
++c;
//最小HashEntry表的數量
int cap = MIN_SEGMENT_TABLE_CAPACITY;
while (cap < c)
cap <<= 1;
//segment初始化
Segment<K,V> s0 =
new Segment<K,V>(loadFactor, (int)(cap * loadFactor),(HashEntry<K,V>[])new HashEntry[cap]);//初始化每個segment的長度
Segment<K,V>[] ss = (Segment<K,V>[])new Segment[ssize]; //初始化segment數組
UNSAFE.putOrderedObject(ss, SBASE, s0);
this.segments = ss;
}
get操作
public V get(Object key) {
Segment<K,V> s;
HashEntry<K,V>[] tab;
//根據key的值計算hash值
int h = hash(key);
//獲得segment的index
long u = (((h >>> segmentShift) & segmentMask) << SSHIFT) + SBASE;
if ((s = (Segment<K,V>)UNSAFE.getObjectVolatile(segments, u)) != null && //通過hash值定位segment中對應的HashEntry 遍歷HashEntry,如果key存在,返回key對應的value 如果不存在則返回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;
}
put操作
public V put(K key, V value) {
Segment<K,V> s;
//鍵和值都不能爲空
if (value == null)
throw new NullPointerException();
//計算key的hash值
int hash = hash(key);
//獲得key所屬的segemngt
int j = (hash >>> segmentShift) & segmentMask;
if ((s = (Segment<K,V>)UNSAFE.getObject
(segments, (j << SSHIFT) + SBASE)) == null)
//初試化segment(懶加載模式)
s = ensureSegment(j);
return s.put(key, hash, value, false);
}
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); //定位到具體的HashEntry
for (HashEntry<K,V> e = first;;) { //3
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;
}
獲取size
public int size() {
final Segment<K,V>[] segments = this.segments;
int size;
boolean overflow;
long sum;
long last = 0L;
int retries = -1;
try {
for (;;) {
//RETRIES_BEFORE_LOCK爲不變常量2 嘗試兩次不鎖住Segment的方式來統計每個Segment的大小,如果在統計的過程中Segment的count發生變化,這時候再加鎖統計Segment的count
if (retries++ == RETRIES_BEFORE_LOCK) { //加鎖
for (int j = 0; j < segments.length; ++j)
ensureSegment(j).lock();
}
sum = 0L;
size = 0;
overflow = false;
for (int j = 0; j < segments.length; ++j) {
Segment<K,V> seg = segmentAt(segments, j);
if (seg != null) {
sum += seg.modCount; //2
int c = seg.count;
if (c < 0 || (size += c) < 0)
overflow = true;
}
}
if (sum == last)
break;
last = sum;
}
} finally {
if (retries > RETRIES_BEFORE_LOCK) {
for (int j = 0; j < segments.length; ++j)
segmentAt(segments, j).unlock();
}
}
return overflow ? Integer.MAX_VALUE : size;
}
弱一致性體現
get與containsKey兩個方法幾乎完全一致:他們都沒有使用鎖,而是通過Unsafe對象的getObjectVolatile()方法提供的原子讀語義,來獲得Segment以及對應的鏈表,然後對鏈表遍歷判斷是否存在key相同的節點以及獲得該節點的value。但由於遍歷過程中其他線程可能對鏈表結構做了調整,因此get和containsKey返回的可能是過時的數據,這一點是ConcurrentHashMap在弱一致性上的體現。如果要求強一致性,那麼必須使用Collections.synchronizedMap()方法。
對比
- ConcurrentHashMap中的key和value值都不能爲null,HashMap中key可以爲null,HashTable中key不能爲null。
- ConcurrentHashMap是線程安全的類並不能保證使用了ConcurrentHashMap的操作都是線程安全的!
- ConcurrentHashMap的get操作不需要加鎖,put操作需要加鎖 - put和get都只關心一個segment裏面的hash操作質量也是很高的,如果hash後都存放在同一個segment中,那麼使用這個類的意義就不會很大.