ConcurrentHashMap源碼學(xué)習(xí)

本文基于JDK1.8源碼。

簡述

  1. ConcurrentHashMap是線程安全的HashMap實(shí)現(xiàn),底層通過一個table(Node[]數(shù)組)維護(hù)hash槽。table的size會被強(qiáng)制設(shè)定為2的冪,負(fù)載因子默認(rèn)為0.75,通過sizeCtl記錄當(dāng)前負(fù)載因子下擴(kuò)容閾值。
  2. hash槽的計(jì)算通過hash值的高16位與低16位做^運(yùn)算后,再和table的size做&運(yùn)算計(jì)算而得。
  3. ConcurrentHashMap通過Unsafe+CAS+Sychronized+Node實(shí)現(xiàn)線程安全,在真正轉(zhuǎn)移的時候都會使用sychronized進(jìn)行加鎖。

那些你不得不知道的節(jié)點(diǎn)

節(jié)點(diǎn)總覽

Node

ForwordingNode、TreeBin、TreeNode的父類,核心的字段就是hash值、key、value和用于在鏈表中指向下一個節(jié)點(diǎn)的next。

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;
    }
  ...
}

ForwordingNode

hash值為-1代表當(dāng)前的table正在進(jìn)行復(fù)制遷移,同時標(biāo)識當(dāng)前槽位的節(jié)點(diǎn)已經(jīng)完成遷移工作。ConcurrentHashMap充分發(fā)揮每個線程的作用,當(dāng)T1線程正在遷移時,其它線程如T2不一定自旋等待,根據(jù)當(dāng)前遷移工作分配情況,可能會協(xié)助進(jìn)行復(fù)制遷移(具體由CPU數(shù)量、table大小、遷移進(jìn)度等決定)。

可以發(fā)現(xiàn)ForwardingNode的構(gòu)造函數(shù)中調(diào)用父類的構(gòu)造函數(shù),有效參數(shù)只有hash值為MOVED(-1),同時用nextTable指向擴(kuò)容后的數(shù)組。

static final class ForwardingNode<K,V> extends Node<K,V> {
    final Node<K,V>[] nextTable;
    // 構(gòu)造函數(shù)調(diào)用 
    ForwardingNode(Node<K,V>[] tab) {
        super(MOVED, null, null, null);
        this.nextTable = tab;
    }
  ...
}

TreeBin

hash值為-2,保存指向紅黑樹根節(jié)點(diǎn)的句柄root。同時保存原有鏈表的第一個元素first。

當(dāng)一個槽位上的Node是TreeBin,這便代表著這個槽位長度已經(jīng)超過8,并且將鏈表初始化為紅黑樹。

// 這里的入?yún)門reeBin的first變量
static <K,V> Node<K,V> untreeify(Node<K,V> b) {
    Node<K,V> hd = null, tl = null;
    for (Node<K,V> q = b; q != null; q = q.next) {
        Node<K,V> p = new Node<K,V>(q.hash, q.key, q.val, null);
        if (tl == null)
            hd = p;
        else
            tl.next = p;
        tl = p;
    }
    return hd;
}

TreeNode

紅黑樹節(jié)點(diǎn)。在鏈表轉(zhuǎn)化為紅黑樹后,由于TreeNode繼承Node,所以依然保留了樹化之前的鏈表關(guān)系,以便當(dāng)節(jié)點(diǎn)數(shù)小于8時,重新轉(zhuǎn)換為鏈表。

除了繼承Node的屬性外,另有代表紅黑樹特性的屬性:parent指向父節(jié)點(diǎn)、left指向左節(jié)點(diǎn)、right指向右節(jié)點(diǎn)、布爾值red代表當(dāng)前節(jié)點(diǎn)是紅色還是黑色(根節(jié)點(diǎn)和葉子節(jié)點(diǎn)都是黑色)。

static final class TreeNode<K,V> extends Node<K,V> {
    TreeNode<K,V> parent;  // red-black tree links
    TreeNode<K,V> left;
    TreeNode<K,V> right;
    TreeNode<K,V> prev;    // needed to unlink next upon deletion
    boolean red;

    TreeNode(int hash, K key, V val, Node<K,V> next,
             TreeNode<K,V> parent) {
        super(hash, key, val, next);
        this.parent = parent;
    }
}

如何計(jì)算hash槽位

// 計(jì)算hash值 h為hashCode 
// HASH_BITS值為(1<<31)-1 = Integer.MAX_VALUE = 0x7fffffff
// (高16位^低16位) & HASH_BITS
static final int spread(int h) {
    return (h ^ (h >>> 16)) & HASH_BITS;
}

final V putVal(K key, V value, boolean onlyIfAbsent) {
   ...
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();
                    // hash值和table的size-1 做&運(yùn)算計(jì)算得到hash槽位的index
                    else if (f = tabAt(tab, i = (n - 1) & hash)) == null) {
                if (casTabAt(tab, i, null,
                             new Node<K,V>(hash, key, value, null)))
                    break;                   // no lock when adding to empty bin
            }
          ...
        }
   ...
 }

擴(kuò)容還是轉(zhuǎn)樹

在ConcurretnHashMap中當(dāng)一個槽位的元素?cái)?shù)到達(dá)8后,會判斷當(dāng)前table的長度是否小于64,如果長度小于64會優(yōu)先進(jìn)行數(shù)組擴(kuò)容。

初始化數(shù)組initTable

ConcurrentHashMap的數(shù)組為懶加載,在調(diào)用放置元素之前不會初始化table。當(dāng)sizeCtl為-1,表示有線程正在初始化table。

private final Node<K,V>[] initTable() {
    Node<K,V>[] tab; int sc;
    // 確認(rèn)table還未初始化
    while ((tab = table) == null || tab.length == 0) {
       // 如果sizeCtl<0, 表示當(dāng)前線程在初始化競爭中失敗,自旋等待(yield會放棄CPU時間片)
        if ((sc = sizeCtl) < 0)
            Thread.yield(); // lost initialization race; just spin
        else if (U.compareAndSwapInt(this, SIZECTL, sc, -1)) {
          //SIZECTL:表示當(dāng)前對象的內(nèi)存偏移量,sc表示期望值,-1表示要替換的值,設(shè)定為-1表示要初始化表了
            try {
                if ((tab = table) == null || tab.length == 0) {
                    int n = (sc > 0) ? sc : DEFAULT_CAPACITY;
                    @SuppressWarnings("unchecked")
                    Node<K,V>[] nt = (Node<K,V>[])new Node<?,?>[n];
                    table = tab = nt;
                   // 這里sc賦值為3/4的數(shù)組長度
                    sc = n - (n >>> 2);
                }
            } finally {
                sizeCtl = sc;
            }
            break;
        }
    }
    return tab;
}

put(K key, V value, boolean onlyIfAbsent)

注意擴(kuò)容包括,數(shù)組擴(kuò)容和對應(yīng)位置的樹化。根據(jù)源碼可以發(fā)現(xiàn)在添加元素的時候有大致四種情況:

  1. 數(shù)組尚未初始化,需要先初始化??赡芡瑫r存在一個線程也在put,這時候,兩個線程會競爭下初始化數(shù)組的權(quán)利,失敗的yield()自旋等待。
  2. 數(shù)組已經(jīng)初始化,對應(yīng)hash槽沒有元素,通過CAS操作添加節(jié)點(diǎn)。值得注意的是,往空槽加元素的時候不會加鎖。
  3. 數(shù)組正在擴(kuò)容復(fù)制數(shù)據(jù),當(dāng)前線程會幫忙復(fù)制數(shù)據(jù),幫忙前會判斷nextTab是否已經(jīng)初始化。
  4. 數(shù)組已經(jīng)初始化完成,沒有在擴(kuò)容復(fù)制數(shù)據(jù),對應(yīng)hash槽非空。很好,又有兩種情況,hash槽對應(yīng)的仍然是鏈表,或者已經(jīng)樹化。
    1. 仍然是鏈表:遍歷鏈表,替換舊值(非putIfAbsent)。沒有舊值,就把元素放到最后。同時更新binCount,后續(xù)判斷是否需要樹化。
    2. 已經(jīng)樹化:設(shè)置binCount=2,避免無效樹化。調(diào)用putTreeVal()獲取待更新節(jié)點(diǎn)。
final V putVal(K key, V value, boolean onlyIfAbsent) {
    if (key == null || value == null) throw new NullPointerException();
    // 計(jì)算hash值
    int hash = spread(key.hashCode());
    int binCount = 0;
    for (Node<K,V>[] tab = table;;) {
        Node<K,V> f; int n, i, fh;
        // 構(gòu)造函數(shù)中不會初始化table,在初次使用時進(jìn)行初始化數(shù)組
        if (tab == null || (n = tab.length) == 0)
            tab = initTable();
        // 數(shù)組已經(jīng)初始化,并且對應(yīng)位置無元素,直接放進(jìn)去
        else if ((f = tabAt(tab, i = (n - 1) & hash)) == null) {
            if (casTabAt(tab, i, null,
                         new Node<K,V>(hash, key, value, null)))
                break;                   // no lock when adding to empty bin
        }
        // 上個判斷中獲得了目標(biāo)位置的節(jié)點(diǎn),如果hash值為MOVED,
        // 代表當(dāng)前正在進(jìn)行擴(kuò)容的數(shù)據(jù)復(fù)制階段,當(dāng)前線程也會參與,
        // 允許多線程復(fù)制的功能,來減少數(shù)組的復(fù)制所帶來的性能損失
        else if ((fh = f.hash) == MOVED)
            tab = helpTransfer(tab, f);
        else {
            V oldVal = null;
            synchronized (f) {
                if (tabAt(tab, i) == f) {
                    // fh>0 還是鏈表 轉(zhuǎn)為樹的時候是-2
                    if (fh >= 0) {
                        // 對應(yīng)位置節(jié)點(diǎn)不為null,
                        binCount = 1;
                        // 開始遍歷鏈表,直到找到對應(yīng)
                        for (Node<K,V> e = f;; ++binCount) {
                            K ek;
                            if (e.hash == hash &&
                                ((ek = e.key) == key ||
                                 (ek != null && key.equals(ek)))) {
                                // 根據(jù)hash值找到對應(yīng)的key位置,如果不是putIfAbsent就進(jìn)行value替換
                                // 記下oldValue用于返回
                                oldVal = e.val;
                                if (!onlyIfAbsent)
                                    e.val = value;
                                break;
                            }
                            Node<K,V> pred = e;
                            // 沒找到原有值,且已經(jīng)到隊(duì)列尾巴,直接加到末尾
                            if ((e = e.next) == null) {
                                pred.next = new Node<K,V>(hash, key,
                                                          value, null);
                                break;
                            }
                        }
                    }
                    // 當(dāng)前hash槽對應(yīng)的是樹
                    else if (f instanceof TreeBin) {
                        Node<K,V> p;
                        // 這里設(shè)置為2,確保后面不會樹化?
                        binCount = 2;
                        if ((p = ((TreeBin<K,V>)f).putTreeVal(hash, key,
                                                       value)) != null) {
                            oldVal = p.val;
                            if (!onlyIfAbsent)
                                p.val = value;
                        }
                    }
                }
            }
            // 因?yàn)槭莗ut方法,所以對應(yīng)hash槽的元素會+1或不變,必不可能減少
            // 只需要考慮是否要樹化
            if (binCount != 0) {
                if (binCount >= TREEIFY_THRESHOLD)
                    treeifyBin(tab, i);
                if (oldVal != null)
                    return oldVal;
                break;
            }
        }
    }
    // size加1,里面很復(fù)雜。。
    addCount(1L, binCount);
    return null;
}

決定是擴(kuò)容還是樹化treeifyBin()

決定樹化會加鎖。

private final void treeifyBin(Node<K,V>[] tab, int index) {
    Node<K,V> b; int n, sc;
    if (tab != null) {
        // 判斷數(shù)組長度是否小于64 ,小于64就擴(kuò)容數(shù)組
        if ((n = tab.length) < MIN_TREEIFY_CAPACITY)
            tryPresize(n << 1);
       // 數(shù)組已經(jīng)達(dá)到MIN_TREEIFY_CAPACITY,決定樹化
        else if ((b = tabAt(tab, index)) != null && b.hash >= 0) {
            // 對數(shù)組對應(yīng)索引的根節(jié)點(diǎn)加鎖
            synchronized (b) {
                // 確保當(dāng)前節(jié)點(diǎn)仍然是第一個節(jié)點(diǎn)
                if (tabAt(tab, index) == b) {
                    // 這里拷貝了一份鏈表,并把節(jié)點(diǎn)換成TreeNode(left,right未初始化)
                    // 真正初始化操作在setTabAt
                    TreeNode<K,V> hd = null, tl = null;
                    for (Node<K,V> e = b; e != null; e = e.next) {
                        TreeNode<K,V> p =
                            new TreeNode<K,V>(e.hash, e.key, e.val,
                                              null, null);
                        if ((p.prev = tl) == null)
                            hd = p;
                        else
                            tl.next = p;
                        tl = p;
                    }
                    // 樹化操作,賦值left right
                    setTabAt(tab, index, new TreeBin<K,V>(hd));
                }
            }
        }
    }
}

putTreeNode

final TreeNode<K,V> putTreeVal(int h, K k, V v) {
    Class<?> kc = null;
    boolean searched = false;
    for (TreeNode<K,V> p = root;;) {
        int dir, ph; K pk;
        if (p == null) {
            // 對應(yīng)位置沒有節(jié)點(diǎn),直接放
            first = root = new TreeNode<K,V>(h, k, v, null, null);
            break;
        }
        else if ((ph = p.hash) > h)
            // hash值比當(dāng)前節(jié)點(diǎn)小 之后找left節(jié)點(diǎn)
            dir = -1;
        else if (ph < h)
            // hash值比當(dāng)前節(jié)點(diǎn)大 之后找right節(jié)點(diǎn)
            dir = 1;
        // 到這里代表hash值相等,到達(dá)目標(biāo)位,返回當(dāng)前值
        else if ((pk = p.key) == k || (pk != null && k.equals(pk)))
            return p;
        else if ((kc == null &&
                  (kc = comparableClassFor(k)) == null) ||
                 (dir = compareComparables(kc, k, pk)) == 0) {
            if (!searched) {
                TreeNode<K,V> q, ch;
                searched = true;
                if (((ch = p.left) != null &&
                     (q = ch.findTreeNode(h, k, kc)) != null) ||
                    ((ch = p.right) != null &&
                     (q = ch.findTreeNode(h, k, kc)) != null))
                    return q;
            }
            dir = tieBreakOrder(k, pk);
        }

        TreeNode<K,V> xp = p;
        if ((p = (dir <= 0) ? p.left : p.right) == null) {
            TreeNode<K,V> x, f = first;
            first = x = new TreeNode<K,V>(h, k, v, f, xp);
            if (f != null)
                f.prev = x;
            if (dir <= 0)
                xp.left = x;
            else
                xp.right = x;
            if (!xp.red)
                x.red = true;
            else {
                lockRoot();
                try {
                    root = balanceInsertion(root, x);
                } finally {
                    unlockRoot();
                }
            }
            break;
        }
    }
    assert checkInvariants(root);
    return null;
}

tryPresize()擴(kuò)容數(shù)組

private final void tryPresize(int size) {
    int c = (size >= (MAXIMUM_CAPACITY >>> 1)) ? MAXIMUM_CAPACITY :
        tableSizeFor(size + (size >>> 1) + 1);
    int sc;
    while ((sc = sizeCtl) >= 0) {
        Node<K,V>[] tab = table; int n;
        // 整個table尚未初始化,在putAll方法中,直接會調(diào)用tryPresize,
        // 所以在這里需要判斷tab是否已經(jīng)初始化
        // 初始化后,把sizeCtl設(shè)置為0.75的數(shù)組長度
        if (tab == null || (n = tab.length) == 0) {
            n = (sc > c) ? sc : c;
            if (U.compareAndSwapInt(this, SIZECTL, sc, -1)) {
                try {
                    if (table == tab) {
                        @SuppressWarnings("unchecked")
                        Node<K,V>[] nt = (Node<K,V>[])new Node<?,?>[n];
                        table = nt;
                        // 這里設(shè)置sizeCtl為0.75的size
                        sc = n - (n >>> 2);
                    }
                } finally {
                  // 更新sizeCtl
                    sizeCtl = sc;
                }
            }
        }
        // 什么情況會到這里?已經(jīng)初始化過table,并且新增的元素?cái)?shù)量并沒有超過0.75數(shù)組長度
        else if (c <= sc || n >= MAXIMUM_CAPACITY)
            break; // 此時c已經(jīng)過時,或者c已經(jīng)超過最大值,無法再resize
        else if (tab == table) {
            int rs = resizeStamp(n);
          /*
           * 如果正在擴(kuò)容Table的話,則幫助擴(kuò)容,支路①
           * 否則的話,在支路②開始新的擴(kuò)容
           * 在transfer操作,將第一個參數(shù)的table中的元素,移動到第二個元素的table中去,
           * 雖然此時第二個參數(shù)設(shè)置的是null,但是,在transfer方法中,當(dāng)?shù)诙€參數(shù)為null的時候,
           * 會創(chuàng)建一個兩倍大小的table
           */
            // ①
            if (sc < 0) {
                Node<K,V>[] nt;
                if ((sc >>> RESIZE_STAMP_SHIFT) != rs || sc == rs + 1 ||
                    sc == rs + MAX_RESIZERS || (nt = nextTable) == null ||
                    transferIndex <= 0)
                    break;
                if (U.compareAndSwapInt(this, SIZECTL, sc, sc + 1))
                    transfer(tab, nt);
            }
          // ②
            else if (U.compareAndSwapInt(this, SIZECTL, sc,
                                         (rs << RESIZE_STAMP_SHIFT) + 2))
                transfer(tab, null);
        }
    }
}

擴(kuò)容的核心方法transfer()

private final void transfer(Node<K,V>[] tab, Node<K,V>[] nextTab) {
    int n = tab.length, stride;
    if ((stride = (NCPU > 1) ? (n >>> 3) / NCPU : n) < MIN_TRANSFER_STRIDE) 
        // 這里對stride有最小值限制,避免占用太多CPU資源
        stride = MIN_TRANSFER_STRIDE; // subdivide range
    if (nextTab == null) {            // initiating
        try {
            @SuppressWarnings("unchecked")
            // 如果傳入的nextTab是null(tryPresize中存在這種情況)
            // nextTab初始化為原tab長度的2倍
            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;
    }
    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;
            else if ((nextIndex = transferIndex) <= 0) {
                i = -1;
                advance = false;
            }
            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);
                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)
            advance = casTabAt(tab, i, null, fwd);
        else if ((fh = f.hash) == MOVED)
            advance = true; // already processed
        else {
            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;
                    }
                }
            }
        }
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容