ConcurrentHashMap的源码分析-treeifyBin
生活随笔
收集整理的這篇文章主要介紹了
ConcurrentHashMap的源码分析-treeifyBin
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在putVal的最后部分,有一個判斷,如果鏈表長度大于8,那么就會觸發擴容或者紅黑樹的轉化操作。
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的長度是不是小于64,如果是,則執行擴容 tryPresize(n << 1); else if ((b = tabAt(tab, index)) != null && b.hash >= 0) {//否則,將當前鏈表轉化為紅黑樹結構存儲 synchronized (b) {// 將鏈表轉換成紅黑樹 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)); } } } } }?
總結
以上是生活随笔為你收集整理的ConcurrentHashMap的源码分析-treeifyBin的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ConcurrentHashMap的源码
- 下一篇: ConcurrentHashMap的源码