-
Notifications
You must be signed in to change notification settings - Fork 267
Expand file tree
/
Copy pathLongAccumulatorDemo.java
More file actions
executable file
·34 lines (29 loc) · 1.03 KB
/
Copy pathLongAccumulatorDemo.java
File metadata and controls
executable file
·34 lines (29 loc) · 1.03 KB
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
26
27
28
29
30
31
32
33
34
package com.javaedge.concurrency.atomic;
import java.util.concurrent.atomic.LongAccumulator;
import java.util.function.LongBinaryOperator;
/**
* LongAdder增强版,处理累加之外,可以自行定义其他计算
*
* @author JavaEdge
* @date 2019/10/18
*/
public class LongAccumulatorDemo {
public static void main(String[] args) throws InterruptedException {
LongAccumulator accumulator = new LongAccumulator(new LongBinaryOperator() {
@Override
public long applyAsLong(long left, long right) {
// 返回最大值,这就是自定义的计算
return left < right ? left : right;
}
}, 0);
// 1000个线程
for (int i = 0; i < 1000; i++) {
int finalI = i;
new Thread(() -> {
accumulator.accumulate(finalI); // 此处实际就是执行上面定义的操作
}).start();
}
Thread.sleep(2000L);
System.out.println(accumulator.longValue()); // 打印出结果
}
}