AtomicLong 通过cas提供了非阻塞的api,但是并发量高的时候,cas会导致大量线程自旋从而浪费cpu资源,所以开发者发明了LongAdder来减少自旋

测试代码

public class LongAdderTest {

  public static void main(String[] args) throws InterruptedException {
      LongAdder longAdder = new LongAdder();
      ArrayList<Thread> threads = new ArrayList<>();

    for (int i = 0; i < 100; i++) {
        Thread thread =
        new Thread(
                () -> {
                    longAdder.add(1);
                    System.out.println(longAdder.longValue());
                });
        threads.add(thread);
    }


      threads.forEach(Thread::start);
      threads.forEach((thread) -> {
          try {
              thread.join();
          } catch (InterruptedException e) {
              e.printStackTrace();
          }
      });
  }
}

longAdder.add方法代码解析

  public void add(long x) {
        Cell[] as; long b, v; int m; Cell a;
        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);
        }
    }


    /**
     * Handles cases of updates involving initialization, resizing,
     * creating new Cells, and/or contention. See above for
     * explanation. This method suffers the usual non-modularity
     * problems of optimistic retry code, relying on rechecked sets of
     * reads.
     *
     * @param x the value
     * @param fn the update function, or null for add (this convention
     * avoids the need for an extra field or function in LongAdder).
     * @param wasUncontended false if CAS failed before call
     */
    final void longAccumulate(long x, LongBinaryOperator fn,
                              boolean wasUncontended) {
        int h;
        if ((h = getProbe()) == 0) {
            ThreadLocalRandom.current(); // force initialization
            h = getProbe();
            wasUncontended = true;
        }
        boolean collide = false;                // True if last slot nonempty
        for (;;) {
            Cell[] as; Cell a; int n; long v;
            if ((as = cells) != null && (n = as.length) > 0) {
                if ((a = as[(n - 1) & h]) == null) {
                    if (cellsBusy == 0) {       // Try to attach new Cell
                        Cell r = new Cell(x);   // Optimistically create
                        if (cellsBusy == 0 && casCellsBusy()) { 
                            boolean created = false;
                            try {               // Recheck under lock
                                Cell[] rs; int m, j;
                                if ((rs = cells) != null &&
                                    (m = rs.length) > 0 &&
                                    rs[j = (m - 1) & h] == null) { // (m - 1) & 位置的cell值为空,则赋值
                                    rs[j] = r;
                                    created = true;
                                }
                            } finally {
                                cellsBusy = 0;
                            }
                            if (created)
                                break;
                            continue;           // Slot is now non-empty
                        }
                    }
                    collide = false;
                }
                else if (!wasUncontended)       // CAS already known to fail
                    wasUncontended = true;      // Continue after rehash
                else if (a.cas(v = a.value, ((fn == null) ? v + x :
                                             fn.applyAsLong(v, x)))) // (m - 1) & 位置的cell值不为空,则cas累加
                    break;
                else if (n >= NCPU || cells != as)
                    collide = false;            // At max size or stale
                else if (!collide)
                    collide = true;
                else if (cellsBusy == 0 && casCellsBusy()) { // 扩容
                    try {
                        if (cells == as) {      // Expand table unless stale
                            Cell[] rs = new Cell[n << 1];
                            for (int i = 0; i < n; ++i)
                                rs[i] = as[i];
                            cells = rs;
                        }
                    } finally {
                        cellsBusy = 0;
                    }
                    collide = false;
                    continue;                   // Retry with expanded table
                }
                h = advanceProbe(h);
            }
            else if (cellsBusy == 0 && cells == as && casCellsBusy()) { // 初始化cell数组
                boolean init = false;
                try {                           // Initialize table
                    if (cells == as) {
                        Cell[] rs = new Cell[2];
                        rs[h & 1] = new Cell(x);
                        cells = rs;
                        init = true;
                    }
                } finally {
                    cellsBusy = 0;
                }
                if (init)
                    break;
            }
            else if (casBase(v = base, ((fn == null) ? v + x :
                                        fn.applyAsLong(v, x))))
                break;                          // Fall back on using base
        }
    }

LongAdder,LongAccumulator 一句话:线程过多,争夺同一个资源时,会将资源拆成一个数组,每个线程操作时会被随机分配到其中一个数组, 获取值时,将所有数组的值加起来即可,这样做法的优点就是降低了线程争抢共享资源的可能性,cas时间也会大大减小。 原理:

  • 当线程较多,出现并发时,
    • 内置的cells数组会被初始化,
      • 如果cell数组上的值如果为null,就将add的值设置上去,
      • 如果cell数组上的值不为nul,会进行cas操作,
        • 如果设置值的时候cas成功
          • 将add的值和原先的值设置到原来位置上
        • 如果设置值的时候cas失败
          • 如果cells数组大小小于cpu个数,则进行扩容