ConcurrentHashMap的源码分析-treeifyBin
生活随笔
收集整理的這篇文章主要介紹了
ConcurrentHashMap的源码分析-treeifyBin
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
在putVal的最后部分,有一個(gè)判斷,如果鏈表長(zhǎng)度大于8,那么就會(huì)觸發(fā)擴(kuò)容或者紅黑樹的轉(zhuǎn)化操作。
private final void treeifyBin(Node<K,V>[] tab, int index) { Node<K,V> b; int n, sc; if (tab != null) { if ((n = tab.length) < MIN_TREEIFY_CAPACITY) //tab的長(zhǎng)度是不是小于64,如果是,則執(zhí)行擴(kuò)容 tryPresize(n << 1); else if ((b = tabAt(tab, index)) != null && b.hash >= 0) {//否則,將當(dāng)前鏈表轉(zhuǎn)化為紅黑樹結(jié)構(gòu)存儲(chǔ) synchronized (b) {// 將鏈表轉(zhuǎn)換成紅黑樹 if (tabAt(tab, index) == b) { 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; } setTabAt(tab, index, new TreeBin<K,V>(hd)); } } } } }?
總結(jié)
以上是生活随笔為你收集整理的ConcurrentHashMap的源码分析-treeifyBin的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ConcurrentHashMap的源码
- 下一篇: ConcurrentHashMap的源码