比LongAddr功能更强大的LongAccumulator原子类原理探究

面试题

(1)LongAccumulator与LongAddr类的结构

(2)LongAddr与LongAccumulator类有什么区别?

(3)LongAddr与LongAccumulator类相同点?

(1)LongAccumulator与LongAddr类的结构

b83e29be72f5cc4e3436ec1c1adf8cc93ca.jpg9e39ddf79e351ca79b80dea9ee66afe9db9.jpg

(2)LongAddr与LongAccumulator类有什么区别?

LongAccumulator类原理分析

/**
 * Creates a new instance using the given accumulator function
 * and identity element.
 * @param accumulatorFunction a side-effect-free function of two arguments
 * @param identity identity (initial value) for the accumulator function
 */
public LongAccumulator(LongBinaryOperator accumulatorFunction,
                       long identity) {
    this.function = accumulatorFunction;
    base = this.identity = identity;
}
@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);
}

首先LongAccumulator类相比于LongAddr功能更加强大,如上代码accumulatorFunction是一个双目运算器接口,其根据输入的两个参数返回一个计算值,identity则是LongAccumulator累加器的初始值。

如何使用LongAccumulator

LongAdder longAdder = new LongAdder();
new LongAccumulator(new LongBinaryOperator() {
    @Override
    public long applyAsLong(long left, long right) {
        return left+right;
    }
},0);

LongAccumulator相比于LongAdder,可以为累加器提供非0的初始值,而LongAdder只能提供默认的0值。

另外,LongAccumulator还可以指定累加规则,比如累加或者相乘,只需要在构造LongAccumulator时,传入自定义的双目运算器即可,后者则内置累加规则。

LongAddr的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);
    }
}

LongAccumulator的accumulate方法

public void accumulate(long x) {
    Cell[] as; long b, v, r; int m; Cell a;
    if ((as = cells) != null ||
        (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);
    }
}

从上面两段代码可知,LongAccumulator相比于LongAddr不同之处在于调用casBase时;

LongAccumulator使用 r = function.applyAsLong(b = base, x)来计算

LongAddr使用casBase(b = base, b + x)来计算

另外,LongAccumulator在调用 longAccumulate 时传递的是 function ,而LongAddr传递的是null。

final void longAccumulate(long x, LongBinaryOperator fn,
                          boolean wasUncontended) {
  ....
        else if (casBase(v = base, ((fn == null) ? v + x :
                                    fn.applyAsLong(v, x))))
            break;                          // Fall back on using base
    }
}

通过LongAccumulator和LongAddr的longAccumulate()方法可知:当fn为null时就使用v+x加法运算,这时候就等价于LongAddr,当fn不为null时,则使用传递的fn函数计算。

(3)LongAddr与LongAccumulator类相同点?

LongAddr与LongAccumulator类都是使用非阻塞算法CAS实现的,这相比于使用锁实现原子性操作在性能上有很大的提高。

LongAddr类是LongAccumulator类的一个特例,只是LongAccumulator提供了更强大的功能,可以让用户自定义累加规则。

参考书籍:

Java并发编程之美

 

转载于:https://my.oschina.net/u/3995125/blog/3075510

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值