1. /**
    2. * Moves and/or copies the nodes in each bin to new table. See
    3. * above for explanation.
    4. */
    5. private final void transfer(Node<K,V>[] tab, Node<K,V>[] nextTab) {//tab 原数组 nextTab 新数组
    6. int n = tab.length, stride;
    7. if ((stride = (NCPU > 1) ? (n >>> 3) / NCPU : n) < MIN_TRANSFER_STRIDE)
    8. stride = MIN_TRANSFER_STRIDE; // subdivide range
    9. if (nextTab == null) { // initiating 如果新数组为空,则初始化新数组
    10. try {
    11. @SuppressWarnings("unchecked")
    12. Node<K,V>[] nt = (Node<K,V>[])new Node<?,?>[n << 1];//容量乘以2
    13. nextTab = nt;
    14. } catch (Throwable ex) { // try to cope with OOME
    15. sizeCtl = Integer.MAX_VALUE;
    16. return;
    17. }
    18. nextTable = nextTab;
    19. transferIndex = n;
    20. }
    21. int nextn = nextTab.length;
    22. ForwardingNode<K,V> fwd = new ForwardingNode<K,V>(nextTab);
    23. boolean advance = true;
    24. boolean finishing = false; // to ensure sweep before committing nextTab
    25. for (int i = 0, bound = 0;;) { //开始搬迁
    26. Node<K,V> f; int fh;
    27. while (advance) {
    28. int nextIndex, nextBound;
    29. if (--i >= bound || finishing)
    30. advance = false;
    31. else if ((nextIndex = transferIndex) <= 0) {
    32. i = -1;
    33. advance = false;
    34. }
    35. else if (U.compareAndSwapInt
    36. (this, TRANSFERINDEX, nextIndex,
    37. nextBound = (nextIndex > stride ?
    38. nextIndex - stride : 0))) {
    39. bound = nextBound;
    40. i = nextIndex - 1;
    41. advance = false;
    42. }
    43. }
    44. if (i < 0 || i >= n || i + n >= nextn) {
    45. int sc;
    46. if (finishing) {
    47. nextTable = null;
    48. table = nextTab;
    49. sizeCtl = (n << 1) - (n >>> 1);
    50. return;
    51. }
    52. if (U.compareAndSwapInt(this, SIZECTL, sc = sizeCtl, sc - 1)) {
    53. if ((sc - 2) != resizeStamp(n) << RESIZE_STAMP_SHIFT)
    54. return;
    55. finishing = advance = true;
    56. i = n; // recheck before commit
    57. }
    58. }
    59. else if ((f = tabAt(tab, i)) == null)
    60. //如果已经处理完了,就会把链表头替换为fwd(相当于一个标志位,表示处理完了)
    61. advance = casTabAt(tab, i, null, fwd);
    62. else if ((fh = f.hash) == MOVED)
    63. //如果已经是fwd (MOVED) 了,则进入下一轮循环
    64. advance = true; // already processed
    65. else {
    66. synchronized (f) {
    67. if (tabAt(tab, i) == f) {
    68. Node<K,V> ln, hn;
    69. if (fh >= 0) {
    70. // >= 0 表示为普通节点
    71. int runBit = fh & n;
    72. Node<K,V> lastRun = f;
    73. for (Node<K,V> p = f.next; p != null; p = p.next) {
    74. int b = p.hash & n;
    75. if (b != runBit) {
    76. runBit = b;
    77. lastRun = p;
    78. }
    79. }
    80. if (runBit == 0) {
    81. ln = lastRun;
    82. hn = null;
    83. }
    84. else {
    85. hn = lastRun;
    86. ln = null;
    87. }
    88. for (Node<K,V> p = f; p != lastRun; p = p.next) {
    89. int ph = p.hash; K pk = p.key; V pv = p.val;
    90. if ((ph & n) == 0)
    91. ln = new Node<K,V>(ph, pk, pv, ln);
    92. else
    93. hn = new Node<K,V>(ph, pk, pv, hn);
    94. }
    95. setTabAt(nextTab, i, ln);
    96. setTabAt(nextTab, i + n, hn);
    97. setTabAt(tab, i, fwd);
    98. advance = true;
    99. }
    100. else if (f instanceof TreeBin) {
    101. // 为负数(<=-2),表示树节点
    102. TreeBin<K,V> t = (TreeBin<K,V>)f;
    103. TreeNode<K,V> lo = null, loTail = null;
    104. TreeNode<K,V> hi = null, hiTail = null;
    105. int lc = 0, hc = 0;
    106. for (Node<K,V> e = t.first; e != null; e = e.next) {
    107. int h = e.hash;
    108. TreeNode<K,V> p = new TreeNode<K,V>
    109. (h, e.key, e.val, null, null);
    110. if ((h & n) == 0) {
    111. if ((p.prev = loTail) == null)
    112. lo = p;
    113. else
    114. loTail.next = p;
    115. loTail = p;
    116. ++lc;
    117. }
    118. else {
    119. if ((p.prev = hiTail) == null)
    120. hi = p;
    121. else
    122. hiTail.next = p;
    123. hiTail = p;
    124. ++hc;
    125. }
    126. }
    127. ln = (lc <= UNTREEIFY_THRESHOLD) ? untreeify(lo) :
    128. (hc != 0) ? new TreeBin<K,V>(lo) : t;
    129. hn = (hc <= UNTREEIFY_THRESHOLD) ? untreeify(hi) :
    130. (lc != 0) ? new TreeBin<K,V>(hi) : t;
    131. setTabAt(nextTab, i, ln);
    132. setTabAt(nextTab, i + n, hn);
    133. setTabAt(tab, i, fwd);
    134. advance = true;
    135. }
    136. }
    137. }
    138. }
    139. }
    140. }