本文轉自以下連接,因爲原文的排版不是很清晰,這裏進行了從新排版以及部分修改:java
Map 這樣的 Key Value 在軟件開發中是很是經典的結構,經常使用於在內存中存放數據。面試
本篇主要想討論 ConcurrentHashMap 這樣一個併發容器,在正式開始以前我以爲有必要談談 HashMap,沒有它就不會有後面的 ConcurrentHashMap。bootstrap
衆所周知 HashMap 底層是基於 數組 + 鏈表 組成的,不過在 jdk1.7 和 1.8 中具體實現稍有不一樣。在 1.7 中HashMap的數據結構圖以下:數組
咱們先來看看 1.7 中HashMap的實現,源碼以下:安全
這是 HashMap 中比較核心的幾個成員變量;看看分別是什麼意思?數據結構
這裏重點解釋一下負載因子,HashMap中共有四個構造函數,咱們來看一下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(); }
因爲給定的 HashMap 的容量大小是固定的,而從源碼中能夠看到默認初始化時給定的默認容量爲 16,負載因子爲 0.75。Map 在使用過程當中不斷的往裏面存放數據,當數量達到了 16 * 0.75 = 12
就須要將當前 16 的容量進行擴容,而擴容這個過程涉及到 rehash、複製數據等操做,因此很是消耗性能。併發
所以一般建議能提早預估 HashMap 的大小最好,儘可能的減小擴容帶來的性能損耗。根據代碼能夠看到其實真正存放數據的數組是:app
transient Entry<K,V>[] table = (Entry<K,V>[]) EMPTY_TABLE;
那麼這個數組,它又是如何定義的呢?以下:
Entry 是 HashMap 中的一個內部類,從他的成員變量很容易看出:
知曉了基本結構後,那咱們來看看其中最爲重要的寫入以及獲取函數。
一、put 方法:
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; }
源碼說明:
二、addEntry與createEntry方法:
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++; }
源碼說明:
三、get 方法:
再來看看 get 方法,以及該方法內調用的 getEntry 方法:
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; }
源碼說明:
不知道從 1.7 的實現你們看出須要優化的點沒有?其實一個很明顯須要優化的地方就是:
當 Hash 衝突嚴重時,在桶上造成的鏈表會變的愈來愈長,這樣在查詢時的效率就會愈來愈低;時間複雜度爲 O(N)。
所以在 1.8 中重點優化了這個查詢效率。在 1.8 中 HashMap 的結構圖:
咱們先來看看在1.8中HashMap幾個核心的成員變量:
/** * The default initial capacity - MUST be a power of two. */ 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; /** * The bin count threshold for using a tree rather than list for a * bin. Bins are converted to trees when adding an element to a * bin with at least this many nodes. The value must be greater * than 2 and should be at least 8 to mesh with assumptions in * tree removal about conversion back to plain bins upon * shrinkage. */ static final int TREEIFY_THRESHOLD = 8; /** * The table, initialized on first use, and resized as * necessary. When allocated, length is always a power of two. * (We also tolerate length zero in some operations to allow * bootstrapping mechanics that are currently not needed.) */ 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 大致上都差很少,仍是有幾個重要的區別:
Node 的核心組成其實也是和 1.7 中的 HashEntry 同樣,存放的都是 key value hashcode next 等數據。
而後咱們再來看看核心方法。
一、put 方法(put裏調用的是putVal):
看似要比 1.7 的複雜,咱們一步步進行拆解:
二、get 方法(get 裏調用的是getNode):
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 正好是環形鏈表的下標就會出現死循環。
咱們先來看單線程下的rehash過程,以下圖:
多線程併發下的rehash過程,以下圖:
還有一個值得注意的是 HashMap 的遍歷方式,一般有如下幾種:
public static void main(String[] args) { Map<String, Integer> map = new HashMap<>(); map.put("a", 1); map.put("b", 2); 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)); } // 等同於第一種方式 for (Map.Entry<String, Integer> entry : map.entrySet()) { System.out.println("key=" + entry.getKey() + " value=" + entry.getValue()); } }
強烈建議使用 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 方法。
put 方法:
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() 自旋獲取鎖。
源碼說明:
再結合圖看看 put 的流程:
get 方法:
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 邏輯比較簡單:
1.7 已經解決了併發問題,而且能支持 N 個 Segment 這麼屢次數的併發,但依然存在 HashMap 在 1.7 版本中的問題。那就是查詢遍歷鏈表效率過低。所以 1.8 作了一些數據結構上的調整。
首先來看下底層的組成結構:
看起來是否是和 1.8 HashMap 結構相似?其中拋棄了原有的 Segment 分段鎖,而採用了 CAS + synchronized 來保證併發安全性:
也將 1.7 中存放數據的 HashEntry 改成 Node,但做用都是相同的。其中的 val next 都用了 volatile 修飾,保證了可見性。
重點來看看 put 方法:
源碼說明:
get 方法:
源碼說明:
1.8 在 1.7 的數據結構上作了大的改動,採用紅黑樹以後能夠保證查詢效率(O(logn)),甚至取消了 ReentrantLock 改成了 synchronized,這樣能夠看出在新版的 JDK 中對 synchronized 優化是很到位的。
看完了整個 HashMap 和 ConcurrentHashMap 在JDK 1.7 和 1.8 中不一樣的實現方式相信你們對他們的理解應該會更加到位。其實這塊也是面試的重點內容,一般的套路是:
這一串問題相信你們仔細看完都能懟回面試官。除了面試會問到以外平時的應用其實也蠻多,像以前談到的 Guava 中 Cache 的實現就是利用 ConcurrentHashMap 的思想。同時也能學習 JDK 做者大牛們的優化思路以及併發解決方案。