当前位置:网站首页>JUC atomic accumulator
JUC atomic accumulator
2022-06-13 09:05:00 【Q z1997】
JUC Atomic accumulator
Accumulator performance comparison
private static <T> void demo(Supplier<T> adderSupplier, Consumer<T> action) {
T adder = adderSupplier.get();
long start = System.nanoTime();
List<Thread> ts = new ArrayList<>();
// 4 Threads , Everyone adds up 50 ten thousand
for (int i = 0; i < 40; i++) {
ts.add(new Thread(() -> {
for (int j = 0; j < 500000; j++) {
action.accept(adder);
}
}));
}
ts.forEach(t -> t.start());
ts.forEach(t -> {
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
});
long end = System.nanoTime();
System.out.println(adder + " cost:" + (end - start)/1000_000);
}
Compare AtomicLong And LongAdder
for (int i = 0; i < 5; i++) {
demo(() -> new LongAdder(), adder -> adder.increment());
}
for (int i = 0; i < 5; i++) {
demo(() -> new AtomicLong(), adder -> adder.getAndIncrement());
}
Output
1000000 cost:43
1000000 cost:9
1000000 cost:7
1000000 cost:7
1000000 cost:7
1000000 cost:31
1000000 cost:27
1000000 cost:28
1000000 cost:24
1000000 cost:22
The reason for the performance improvement is simple , It's when there's competition , Set multiple accumulation units ,Therad-0 Add up Cell[0], and Thread-1 Add up Cell[1]… Finally, summarize the results . In this way, they operate differently when accumulating Cell Variable , Therefore, it is reduced CAS Retry fail , To improve performance .
边栏推荐
- 20211104 why are the traces of similar matrices the same
- Knowledge points related to system architecture 1
- Uni app essay
- What are the bank financial products? How long is the liquidation period?
- 你不知道的stringstream的用法
- Pytorch same structure different parameter name model loading weight
- Completely uninstall PostgreSQL under Linux
- 20211108 det(AB)=det(A)det(B)
- output. Interpretation of topk() function
- Onnx crop intermediate node
猜你喜欢

20211104 why are the traces of similar matrices the same

Message Oriented Middleware

Detailed explanation of C language callback function

Cesium achieves sunny, rainy, foggy, snowy and other effects

transforms. ColorJitter(0.3, 0, 0, 0)

Simulink的Variant Model和Variant Subsystem用法

【安全】零基礎如何從0到1逆襲成為安全工程師

「解读」华为云桌面说“流畅”的时候,究竟在说什么?

Animation through svg

Completely uninstall PostgreSQL under Linux
随机推荐
20211104 为什么矩阵的迹等于特征值之和,为什么矩阵的行列式等于特征值之积
[QNX hypervisor 2.2 user manual] 4.5.1 build QNX guest
Cesium displays a pop-up box at the specified position and moves with the map
JUC 字段更新器
strcpy_ S precautions for use. (do not use strcpy_s where memcpy_s can be used)
消息中间件
Number of parameters of pytorch statistical model
JUC原子整数
20211104 why are the traces of similar matrices the same
20211115 矩阵对角化的充要条件;满秩矩阵不一定有n个线性无关的特征向量;对称矩阵一定可以对角化
Three indexes reflecting system reliability in performance test: MTTF, MTTR and MTBF
Implement authentication code login and remember password (cookie)
教程篇(5.0) 03. 安全策略 * FortiEDR * Fortinet 网络安全专家 NSE 5
网络安全漏洞分析之重定向漏洞分析
Object in ES6 Use of entries()
Uni app essay
【安全】零基礎如何從0到1逆襲成為安全工程師
20211104 为什么相似矩阵的迹相同
QObject::connect: Cannot queue arguments of type ‘QTextCursor‘ (Make sure ‘QTextCursor‘ is registere
Qvector shallow copy performance test