LongAccumulator源码解析

LongAccumulator源码解析

LongAccumulator类介绍

LongAccumulator也是JDK提供的基于Striped64实现的线程安全的long累加器。

类图

LongAccumulator类图

主要属性

1
2
private final LongBinaryOperator function;
private final long identity;

function:二元函数

identity:其实就是LongAdder中的base,它是将double类型的初始值转为long保存。

主要方法

LongAccumulator(LongBinaryOperator,long)

1
2
3
4
5
6
7
8
9
10
11
/**
* 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;
}

初始化一个LongAccumulator对象。

accumulate(long)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
* Updates with the given value.
*
* @param x the value
*/
public void accumulate(long x) {
Cell[] as; long b, v, r; int m; Cell a;
// 如果cells数组不为null,说明存在竞态条件
// 或者,将base更换为当前function.applyAsLong(base,x)失败
if ((as = cells) != null ||
(r = function.applyAsLong(b = base, x)) != b && !casBase(b, r)) {
// 存在竞态条件,设置为true
boolean uncontended = true;
// 如果当前cell数组为null,或者长度为0,又或者function.applyAsLong(base,x)==base,或者cas(base, function.applyAsLong(x))失败,则调用父类Strip64的longAccumulate方法。
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);
}
}

get()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
* Returns the current value. The returned value is <em>NOT</em>
* an atomic snapshot; invocation in the absence of concurrent
* updates returns an accurate result, but concurrent updates that
* occur while the value is being calculated might not be
* incorporated.
*
* @return the current value
*/
public long get() {
Cell[] as = cells; Cell a;
long result = base;
if (as != null) {
for (int i = 0; i < as.length; ++i) {
if ((a = as[i]) != null)
result = function.applyAsLong(result, a.value);
}
}
return result;
}

获取base和当前所有cell的累加和

reset()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
* Resets variables maintaining updates to the identity value.
* This method may be a useful alternative to creating a new
* updater, but is only effective if there are no concurrent
* updates. Because this method is intrinsically racy, it should
* only be used when it is known that no threads are concurrently
* updating.
*/
public void reset() {
Cell[] as = cells; Cell a;
base = identity;
if (as != null) {
for (int i = 0; i < as.length; ++i) {
if ((a = as[i]) != null)
a.value = identity;
}
}
}

重置base和各个cell的值

getThenReset()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/**
* Equivalent in effect to {@link #get} followed by {@link
* #reset}. This method may apply for example during quiescent
* points between multithreaded computations. If there are
* updates concurrent with this method, the returned value is
* <em>not</em> guaranteed to be the final value occurring before
* the reset.
*
* @return the value before reset
*/
public long getThenReset() {
Cell[] as = cells; Cell a;
long result = base;
base = identity;
if (as != null) {
for (int i = 0; i < as.length; ++i) {
if ((a = as[i]) != null) {
long v = a.value;
a.value = identity;
result = function.applyAsLong(result, v);
}
}
}
return result;
}

获取当前和以及重置

toString()

1
2
3
4
5
6
7
/**
* Returns the String representation of the current value.
* @return the String representation of the current value
*/
public String toString() {
return Long.toString(get());
}

返回当前和的String形式

longValue()

1
2
3
4
5
6
7
8
/**
* Equivalent to {@link #get}.
*
* @return the current value
*/
public long longValue() {
return get();
}

返回当前和

intValue()

1
2
3
4
5
6
7
/**
* Returns the {@linkplain #get current value} as an {@code int}
* after a narrowing primitive conversion.
*/
public int intValue() {
return (int)get();
}

返回当前和的int型。此处要注意溢出,是一个向下转型

floatValue()

1
2
3
4
5
6
7
/**
* Returns the {@linkplain #get current value} as a {@code float}
* after a widening primitive conversion.
*/
public float floatValue() {
return (float)get();
}

返回当前和的float型。

doubleValue()

1
2
3
4
5
6
7
/**
* Returns the {@linkplain #get current value} as a {@code double}
* after a widening primitive conversion.
*/
public double doubleValue() {
return (double)get();
}

返回当前和的double型。