透過陣列與值實現,看原始碼
public void add(long x) {
Cell[] as; long b, v; int m; Cell a;
// 嘗試直接透過CAS操作累加base的值
// 如果cells陣列不為null,或者透過CAS操作未能成功累加base的值
//相當於是更新base值,如果失敗了就去更新Cell陣列
if ((as = cells) != null || !casBase(b = base, b + x)) {
boolean uncontended = true;
if (as == null || (m = as.length - 1) < 0 ||
(a = as[getProbe() & m]) == null ||
!(uncontended = a.cas(v = a.value, v + x)))
longAccumulate(x, null, uncontended);
}
}