ConcurrentHashMap的源码分析-tryPresize
生活随笔
收集整理的這篇文章主要介紹了
ConcurrentHashMap的源码分析-tryPresize
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
tryPresize里面部分代碼和addCount的部分代碼類似,看起來會稍微簡單一些
private final void tryPresize(int size) {//對size進行修復,主要目的是防止傳入的值不是一個2次冪的整數,然后通過tableSizeFor來講入參轉化為離該整數最近的2次冪 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;//下面這段代碼和initTable是一樣的,如果table沒有初始化,則開始初始化 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; sc = n - (n >>> 2);//0.75 } } finally { sizeCtl = sc; } } } // else if (c <= sc || n >= MAXIMUM_CAPACITY) break; else if (tab == table) {//這段代碼和addCount后部分代碼是一樣的,做輔助擴容操作 int rs = resizeStamp(n); 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); } } }?
總結
以上是生活随笔為你收集整理的ConcurrentHashMap的源码分析-tryPresize的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ConcurrentHashMap的源码
- 下一篇: 生产者消费者的实际使用