面试题
(1)LongAccumulator与LongAddr类的结构
(2)LongAddr与LongAccumulator类有什么区别?
(3)LongAddr与LongAccumulator类相同点?
(1)LongAccumulator与LongAddr类的结构
(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并发编程之美