DoubleAdder
double累加器,跟LongAdder类似,操作的是double。 但是Striped64的base是long型,需要Double.doubleToRawLongBits 及Double.longBitsToDouble来进行转换。
构造方法
DoubleAdder()
java
// 初始值为0
public DoubleAdder() {
}
// 初始值为0
public DoubleAdder() {
}
1
2
3
2
3
方法
void add(double x)
java
// 累加double值x
// 跟LongAdder.add()类似
public void add(double x) {
Cell[] as; long b, v; int m; Cell a;
if ((as = cells) != null || // hash表不为null
// 尝试在base上累加失败
!casBase(b = base,
Double.doubleToRawLongBits
(Double.longBitsToDouble(b) + x))) {
// 设置为非竞争
boolean uncontended = true;
if (as == null || (m = as.length - 1) < 0 || // hash表未初始化
// hash表根据hash值计算的索引位置的Cell为null
(a = as[getProbe() & m]) == null ||
// 在Cell a上直接cas累加x失败 设置为竞争
!(uncontended = a.cas(v = a.value,
Double.doubleToRawLongBits
(Double.longBitsToDouble(v) + x))))
// 调用父类的doubleAccumulate
// DoubleBinaryOperator为null是默认为累加
doubleAccumulate(x, null, uncontended);
}
}
// 累加double值x
// 跟LongAdder.add()类似
public void add(double x) {
Cell[] as; long b, v; int m; Cell a;
if ((as = cells) != null || // hash表不为null
// 尝试在base上累加失败
!casBase(b = base,
Double.doubleToRawLongBits
(Double.longBitsToDouble(b) + x))) {
// 设置为非竞争
boolean uncontended = true;
if (as == null || (m = as.length - 1) < 0 || // hash表未初始化
// hash表根据hash值计算的索引位置的Cell为null
(a = as[getProbe() & m]) == null ||
// 在Cell a上直接cas累加x失败 设置为竞争
!(uncontended = a.cas(v = a.value,
Double.doubleToRawLongBits
(Double.longBitsToDouble(v) + x))))
// 调用父类的doubleAccumulate
// DoubleBinaryOperator为null是默认为累加
doubleAccumulate(x, null, uncontended);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
double sum()
java
// 获得当前实例的double值
// 用于Number类的方法longValue、intValue、doubleValue等
// 类似LongAdder.sum()
public double sum() {
Cell[] as = cells; Cell a;
// 获得base的double值
double sum = Double.longBitsToDouble(base);
if (as != null) {
// 累加hash表所有非null的单元
for (int i = 0; i < as.length; ++i) {
if ((a = as[i]) != null)
sum += Double.longBitsToDouble(a.value);
}
}
return sum;
}
// 获得当前实例的double值
// 用于Number类的方法longValue、intValue、doubleValue等
// 类似LongAdder.sum()
public double sum() {
Cell[] as = cells; Cell a;
// 获得base的double值
double sum = Double.longBitsToDouble(base);
if (as != null) {
// 累加hash表所有非null的单元
for (int i = 0; i < as.length; ++i) {
if ((a = as[i]) != null)
sum += Double.longBitsToDouble(a.value);
}
}
return sum;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void reset()
java
// 重置当前实例的double值为0
public void reset() {
Cell[] as = cells; Cell a;
// 重置base值为0
// Double.longBitsToDouble(0L)值为0
base = 0L; // relies on fact that double 0 must have same rep as long
if (as != null) {
// 重置hash表所有非null的单元为0
for (int i = 0; i < as.length; ++i) {
if ((a = as[i]) != null)
a.value = 0L;
}
}
}
// 重置当前实例的double值为0
public void reset() {
Cell[] as = cells; Cell a;
// 重置base值为0
// Double.longBitsToDouble(0L)值为0
base = 0L; // relies on fact that double 0 must have same rep as long
if (as != null) {
// 重置hash表所有非null的单元为0
for (int i = 0; i < as.length; ++i) {
if ((a = as[i]) != null)
a.value = 0L;
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
double sumThenReset()
java
// 获得当前实例的double值并重置
public double sumThenReset() {
Cell[] as = cells; Cell a;
// 获得base的值并重置为0
double sum = Double.longBitsToDouble(base);
base = 0L;
if (as != null) {
// 获得hash表所有非空单元的值并重置为0
for (int i = 0; i < as.length; ++i) {
if ((a = as[i]) != null) {
long v = a.value;
a.value = 0L;
sum += Double.longBitsToDouble(v);
}
}
}
return sum;
}
// 获得当前实例的double值并重置
public double sumThenReset() {
Cell[] as = cells; Cell a;
// 获得base的值并重置为0
double sum = Double.longBitsToDouble(base);
base = 0L;
if (as != null) {
// 获得hash表所有非空单元的值并重置为0
for (int i = 0; i < as.length; ++i) {
if ((a = as[i]) != null) {
long v = a.value;
a.value = 0L;
sum += Double.longBitsToDouble(v);
}
}
}
return sum;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18