LongAdder和AtomicLong性能对比
2024-10-06 10:25:19
jdk1.8中新原子操作封装类LongAdder和jdk1.5的AtomicLong和synchronized的性能对比,直接上代码:
package com.itbac.cas; import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.LongAdder; // 测试用例: 同时运行2秒,检查谁的次数最多
public class LongAdderDemo {
// synchronized 方式
private long count = 0;
// Atomic方式
private AtomicLong acount = new AtomicLong(0L);
// LongAdder 方式 (jdk1.8,新计数器)
private LongAdder lacount = new LongAdder(); // 运行时间,毫秒数
private int time=2000; // 同步代码块的方式
public void testSync() throws InterruptedException {
for (int i = 0; i < 3; i++) {
new Thread(() -> {
long starttime = System.currentTimeMillis();
while (System.currentTimeMillis() - starttime < time) { // 运行两秒
synchronized (this) {
++count;
}
}
long endtime = System.currentTimeMillis();
System.out.println("SyncThread spend:" + (endtime - starttime) + "ms" + " v:" + count);
}).start();
}
} public void testAtomic() throws InterruptedException {
for (int i = 0; i < 3; i++) {
new Thread(() -> {
long starttime = System.currentTimeMillis();
while (System.currentTimeMillis() - starttime < time) { // 运行两秒
acount.incrementAndGet(); // acount++;
}
long endtime = System.currentTimeMillis();
System.out.println("AtomicThread spend:" + (endtime - starttime) + "ms" + " v:" + acount.incrementAndGet());
}).start();
}
} public void testLongAdder() throws InterruptedException {
for (int i = 0; i < 3; i++) {
new Thread(() -> {
long starttime = System.currentTimeMillis();
while (System.currentTimeMillis() - starttime < time) { // 运行两秒
lacount.increment();
}
long endtime = System.currentTimeMillis();
System.out.println("LongAdderThread spend:" + (endtime - starttime) + "ms" + " v:" + lacount.sum());
}).start();
}
} public static void main(String[] args) throws InterruptedException {
LongAdderDemo demo = new LongAdderDemo();
demo.testSync();
demo.testAtomic();
demo.testLongAdder();
}
}
看看输出结果:
SyncThread spend:2000ms v:25458554
SyncThread spend:2000ms v:25458554
SyncThread spend:2000ms v:25458554
AtomicThread spend:2000ms v:78489760
AtomicThread spend:2000ms v:78489759
AtomicThread spend:2000ms v:78489758
LongAdderThread spend:2000ms v:141154988
LongAdderThread spend:2000ms v:141154988
LongAdderThread spend:2000ms v:141352859
jdk版本,作者及类名:
* @since 1.5
* @author Doug Lea
AtomicLong
* @since 1.8
* @author Doug Lea
LongAdder
让我们来膜拜一下大神!2秒破亿次累加。翻倍的性能提升。
最新文章
- DependencyResolver.Current
- C++ 系列:C++ 对象模型
- Linq 操作基础
- 【android-tips】如何在view中取得activity对象
- json格式
- unity3d下载Obb分包文件
- unigui判断浏览器内核、操作系统以及是否移动终端函数
- P1417 烹调方案
- html和css实现一级菜单和二级菜单学习笔记
- Javascript中布尔运算符的高级应用
- js 基础对象二
- X-006 FriendlyARM tiny4412 u-boot移植之Debug串口用起来
- spring mvc 返回页面数据
- EditPlus行首行尾批量添加字符 以及其它常用正则
- 与MP3相关的技术总结
- 304. Range Sum Query 2D - Immutable(动态规划)
- HTML5新规范和CSS3新特性
- 【Python基础】random 的高级玩法
- golang-build-error
- WordPress上传文件类型限制解决办法
热门文章
- 用户点击获取验证码之后我们会发送一条信息到用户手机,然后就会出现一个倒计时按钮,很像支付宝手机付款效果了,下面我给大家分享两个js效果
- 用python的matplotlib和numpy库绘制股票K线均线和成交量的整合效果(含量化验证交易策略代码)
- @Bean 注解全解析
- Elasticsearch(一)开启外网访问
- Spring Boot + Elasticsearch 实现索引的日常维护
- ElasticStack学习(九):深入ElasticSearch搜索之词项、全文本、结构化搜索及相关性算分
- php常用实用函数整理
- Linux 系统的基本操作及工具的使用
- Pascal到c++,求大佬翻译!
- 【SpringCloud】Ribbon如何自定义客户端配置和全局配置