天天看点

LongAdder源码分析(只分析了两个方法,add和longAccumulate,也是核心方法)

    public void add(long x) {

        Cell[] as; long b, v; int m; Cell a;

            if ((as = cells) != null || !casBase(b = base, b + x)) {//★三种情况,这里的cas只尝试一次,(在cells为空的情况下)失败就进去

                                                                        1.cells为空,cas(false||false)成功就不进去; 

                                                                        2.cells为空且casBase失败(false||true),要进去初始化cells     ▲

                                                                        3.cells不为空(true||{true|false}),肯定要进去尝试优化      ▲

            boolean uncontended = true;//★初始化未争用标识

            if (as == null || (m = as.length - 1) < 0 ||

                (a = as[getProbe() & m]) == null ||        //★获取当前线程的probe对应的cell,如果为空得初始化

                !(uncontended = a.cas(v = a.value, v + x)))//★uncontended这个是对线程对应的cell的值进行加一操作的结果,

                                                            冲突说明了什么?hash到同一个cell的多个线程同时操作cas,得进入下一个方法处理

                longAccumulate(x, null, uncontended);

        }

    }

    final void longAccumulate(long x, LongBinaryOperator fn,

                              boolean wasUncontended) {

        int h;

        if ((h = getProbe()) == 0) {//★第一次进入,如果hash为0,必须rehash

            //初始化ThreadLocalRandom;

            ThreadLocalRandom.current(); 

            //将h设置为0x9e3779b9

            h = getProbe();

            //设置未竞争标记为true

            wasUncontended = true;//★设置hash到同一个cell上并尝试cas这个cell的value时产生的竞争,竞争改value

        }

        boolean collide = false; //★修改cells起冲突了 

        for (;;) {

            Cell[] as; Cell a; int n; long v;

             if ((as = cells) != null && (n = as.length) > 0) {//★cell为空或者数组非空,可以尝试累加

                if ((a = as[(n - 1) & h]) == null) {//★这个cell还是virgin

                    if (cellsBusy == 0) {//★cellsbusy是个自旋锁,判断是否可以对cells数组进行操作

                        Cell r = new Cell(x); 

                        if (cellsBusy == 0 && casCellsBusy()) {//★获取该cell的自旋锁成功

                            boolean created = false;

                            try {

                                Cell[] rs; int m, j;

                                if ((rs = cells) != null &&

                                    (m = rs.length) > 0 &&

                                    rs[j = (m - 1) & h] == null) {//★再次检查是不是virgin

                                    rs[j] = r;                    //★ 将该插入的插入

                                    created = true;

                                }

                            } finally {

                                cellsBusy = 0;//★自旋锁放开,别人可以对cells数组进行修改操作了

                            }

                            if (created)//★爽完了,可以跳出了

                                break;

                            continue;//★没有获取锁,被别人爽了,继续循环

                        }

                    }

                    collide = false;//★有别的线程获取了这个cells数组的锁,不能改了,重置collide,继续下一个循环

                } else if (!wasUncontended)  

                    wasUncontended = true;//★修改cell的value竞争了,设置标志位,重新循环

                else if (a.cas(v = a.value, ((fn == null) ? v + x ://★特殊情况排除,就尝试累加cell,尝试成功就返回

                                             fn.applyAsLong(v, x))))

                    break;

                else if (n >= NCPU || cells != as)//★上一步争用失败,数组越界,或者cells被修改,重置冲突标志位

                    collide = false;

                else if (!collide)

                    collide = true;//★上面全部处理失败,肯定有冲突,将冲突标志位设置为true

                else if (cellsBusy == 0 && casCellsBusy()) {//★处理扩容(2倍)cells数组情况,获取了cellsbusy锁,并且他不忙

                    try {

                        if (cells == as) {      //★有可能人家改了,又释放了锁,检查cells还是不是以前那个数组

                            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;           //★扩容完还得尝试累加value        // Retry with expanded table

                }

                h = advanceProbe(h);//★上面出问题的条件语句,都必须执行这里rehash

            }else if (cellsBusy == 0 && cells == as && casCellsBusy()) {//★未初始化且获取了cells的自旋锁就初始化

                boolean init = false;

                try {                           // Initialize table

                    //初始化cells数组,初始容量为2,并将x值通过hash&1,放到0个或第1个位置上

                    if (cells == as) {

                        Cell[] rs = new Cell[2];

                        rs[h & 1] = new Cell(x);

                        cells = rs;

                        init = true;

                    }

                } finally {

                    //解锁

                    cellsBusy = 0;

                }

                //如果init为true说明初始化成功,跳出循环

                if (init)//★初始化成功

                    break;

            }

            else if (casBase(v = base, ((fn == null) ? v + x :

                                        fn.applyAsLong(v, x))))//★最次的情况,只能在base上尝试累加了,也就退化成atomicLong类型了

                break;                          // Fall back on using base

        }

    }

继续阅读