1、LongAccumulator類
1.和LongAdder之間的關係
-
LongAdder類是LongAccumulator的一個特例,咱們看一下LongAccumulator的一個構造方法
public LongAccumlator(LongBinaryOperator accumulatorFunction,long identity) {
this.function = accumulatorFunction;
base = this.identity = identity;
}
-
其中參數identity是累加器的初始值;參數類型LongBinaryOperator是一種至關於二目運算符的類,它的輸入是兩個Long型數,返回也是一個Long型數字,咱們看一下這個接口定義
public interface LongBinaryOperator {
long applyAsLong(long left,long right);
}
LongAdder adder = new LongAdder();
LongAccumulator accumulator = new LongAccumulator(new LongBinaryOperator() {
@Override
public long applyAsLong(long left,long right) {
return left + right;
}
}
-
LongAccumulator相比於LongAdder,能夠爲累加器提供非0的初始值,後者只能從0開始累加,而且前者能夠自定義累加規則,咱們只須要實現這個接口,而後在接口內部的方法內,自定累加規則便可。
-
從下面的代碼看一下LongAccumulator的accumulate方法和LongAdder類的add方法
public void add(long x){
Cell[] as;
long b;
long 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))){
longAccumulator(x,null,uncontended);
}
}
}
public void accumulate(long x){
Cell[] as;
long b;
long v;
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))){
longAccumulator(x,null,uncontended);
}
}
-
調用casBase的時候後者傳遞的是b+x,前者使用了r=function.applyAsLong(b = base,x)來計算
-
前者在調用longAccumulator時傳遞的是function,然後者是null,從下面的代碼看出
else if(casBase(v = base,((fn==null)?v+x:fn.applyAsLong(v,x)))){
break;
}
-
當fu爲null時,就是用了x+v的加法運算,這時候等價於LongAdder,當fn不爲null則使用傳遞的fu函數計算
2.總結:能夠看到該類提供的功能更加通常化
2、源碼:
-
所在包:com.ruigege.AtomicOperationClass4
-
https://github.com/ruigege66/ConcurrentJava
-
-
-
歡迎關注微信公衆號:傅里葉變換,我的帳號,僅用於技術交流