比LongAddr功能更強大的LongAccumulator原子類原理探究
面試題
(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併發程式設計