美文网首页
[Java]重学Java-LongAccumulator 类

[Java]重学Java-LongAccumulator 类

作者: AbstractCulture | 来源:发表于2022-11-06 17:19 被阅读0次

    LongAccumulator

    LongAdder类是LongAccumulator的一个特例,它提供给用户一个自定义规则的可能——accumulatorFunction.

    public LongAccumulator(LongBinaryOperator accumulatorFunction,
                           long identity) {
        this.function = accumulatorFunction;
        base = this.identity = identity;
    }
    
    • java.util.function.LongBinaryOperator
    @FunctionalInterface
    public interface LongBinaryOperator {
    
        /**
         * Applies this operator to the given operands.
         *
         * @param left the first operand
         * @param right the second operand
         * @return the operator result
         */
        long applyAsLong(long left, long right);
    }
    

    LongBinaryOperator是一个lambda表达式接口,(a,b)->c,输入left和right返回一个long值.

    LongAccumulator可以实现初始化自定义,LongAdder默认是0,同时,通过LongBinaryOperator,我们可以实现一些计算过程.

    LongAccumulator示例

    package com.tea.modules.java8.thread.atomic;
    
    import java.util.concurrent.atomic.LongAccumulator;
    import java.util.concurrent.atomic.LongAdder;
    import java.util.function.LongBinaryOperator;
    
    /**
     * com.tea.modules.java8.thread.atomic
     * LongAccumulator学习
     *
     * @author jaymin
     * @since 2022/11/7
     */
    public class LongAccumulatorDemo {
        public static void main(String[] args) {
            // left代表base的值,也就是1;
            // right代表的是传进来的值,也就下面accumulate函数传的值
            LongAccumulator longAccumulator = new LongAccumulator((left, right) -> left * right, 1);
            longAccumulator.accumulate(2);
            System.out.println(longAccumulator.get());
    
            LongAdder longAdder = new LongAdder();
            longAdder.add(1);
            System.out.println(longAdder.longValue());
    
            LongAccumulator longAdd = new LongAccumulator(Long::sum, 0);
            longAdd.accumulate(1);
            System.out.println(longAdd.get());
        }
    }
    

    这里展示了2个示例,一个是声明一个累乘计算器,一个是用LongAccumulator实现了一个LongAdder.

    源码对比

    • java.util.concurrent.atomic.LongAccumulator#accumulate
    public void accumulate(long x) {
        Cell[] as; long b, v, r; int m; Cell a;
        if ((as = cells) != null ||
            // casBase采用的是lambda表达式返回的值
            (r = function.applyAsLong(b = base, x)) != b && !casBase(b, r)) {
            boolean uncontended = true;
            if (as == null || (m = as.length - 1) < 0 ||
                (a = as[getProbe() & m]) == null ||
                !(uncontended =
                  (r = function.applyAsLong(v = a.value, x)) == v ||
                  a.cas(v, r)))
                longAccumulate(x, function, uncontended);
        }
    }
    
    • java.util.concurrent.atomic.LongAdder#add
    public void add(long x) {
        Cell[] as; long b, v; int m; Cell a;
        // casBase采用的是 b+x
        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);
        }
    }
    

    相关文章

      网友评论

          本文标题:[Java]重学Java-LongAccumulator 类

          本文链接:https://www.haomeiwen.com/subject/pnxitdtx.html