当前位置:网站首页>JUC 原子累加器
JUC 原子累加器
2022-06-13 09:00:00 【Q z1997】
JUC 原子累加器
累加器性能比较
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 个线程,每人累加 50 万
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);
}
比较 AtomicLong 与 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());
}
输出
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
性能提升的原因很简单,就是在有竞争时,设置多个累加单元,Therad-0 累加 Cell[0],而 Thread-1 累加Cell[1]… 最后将结果汇总。这样它们在累加时操作的不同的 Cell 变量,因此减少了 CAS 重试失败,从而提高性能。
边栏推荐
- 13.inline,const,mutable,this,static
- Is it safe to open an account online? Can a novice open an account?
- Object in ES6 Use of entries()
- Talking about acid of database
- Bash: kill: (74001) - operation not allowed
- Web page H5 wechat sharing
- 「解读」华为云桌面说“流畅”的时候,究竟在说什么?
- Pop component submission success failure animation
- ES6 use of dynamic attributes
- 20211104 为什么矩阵的迹等于特征值之和,为什么矩阵的行列式等于特征值之积
猜你喜欢

turf. JS usage

How many TCP connections can a machine create at most?

Collection of garbled code problems in idea development environment

QObject::connect: Cannot queue arguments of type ‘QTextCursor‘ (Make sure ‘QTextCursor‘ is registere

Jfinal and swagger integration

【网络安全渗透】如果你还不懂CSRF?这一篇让你彻底掌握

What exactly is Huawei cloud desktop saying when it says "smooth"?

【网络安全】SQL注入新思维之webshell提权

Tutorial (5.0) 01 Product introduction and installation * fortiedr * Fortinet network security expert NSE 5

教程篇(5.0) 02. 管理 * FortiEDR * Fortinet 网络安全专家 NSE 5
随机推荐
ES6 use of dynamic attributes
Library management system based on wechat applet Rar (thesis + source code)
20211104 why is the trace of a matrix equal to the sum of eigenvalues, and why is the determinant of a matrix equal to the product of eigenvalues
Number of parameters of pytorch statistical model
What exactly is Huawei cloud desktop saying when it says "smooth"?
20211104 why are the traces of similar matrices the same
The Jenkins console does not output custom shell execution logs
strcpy_ S precautions for use. (do not use strcpy_s where memcpy_s can be used)
教程篇(5.0) 03. 安全策略 * FortiEDR * Fortinet 网络安全专家 NSE 5
Jfinal and swagger integration
[security] how to counter attack from 0 to 1 to become a security engineer with zero Foundation
简单实现数据库链接池
类的加载概述
20220524 如何把CoppeliaSim安装到D盘
20211018 some special matrices
Opencv gaussianblur() explanation (Sigma value)
Talking about acid of database
【QNX Hypervisor 2.2 用户手册】4.5 构建Guest
Common network problems and troubleshooting methods of gbase
Message Oriented Middleware