当前位置:网站首页>Three high concurrency methods to realize I++
Three high concurrency methods to realize I++
2022-07-19 05:06:00 【mabo_ [email protected]】
Three high concurrency methods i++
synchronized keyword
package runnable;
public class RunnableTest implements Runnable{
private Object object=new Object();
private int a=0;
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
@Override
public void run() {
synchronized (object){
a++;
}
}
public static void main(String[] args) {
long start = System.currentTimeMillis();
RunnableTest test=new RunnableTest();
for (int i = 0; i < 100000; i++) {
Thread thread = new Thread(test::run);
thread.start();
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Thread threadX = new Thread(new Runnable() {
public void run() {
System.out.println(test.getA());
long end = System.currentTimeMillis();
System.out.println((end-start)+"ms");
}
});
threadX.start();
}
}
AtomicInteger
package atomic;
import java.util.concurrent.atomic.AtomicInteger;
public class AtomicIntegerTest implements Runnable {
AtomicInteger atomicInteger=new AtomicInteger(0);
public AtomicInteger getAtomicInteger() {
return atomicInteger;
}
public void setAtomicInteger(AtomicInteger atomicInteger) {
this.atomicInteger = atomicInteger;
}
@Override
public void run() {
atomicInteger.incrementAndGet();
}
public static void main(String[] args) {
long start = System.currentTimeMillis();
AtomicIntegerTest test=new AtomicIntegerTest();
for (int i = 0; i < 100000; i++) {
Thread thread = new Thread(test::run);
thread.start();
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println(" Add error ");
}
}
Thread threadX = new Thread(new Runnable() {
public void run() {
System.out.println(test.getAtomicInteger());
long end = System.currentTimeMillis();
System.out.println((end-start)+"ms");
}
});
threadX.start();
}
}
LongAdder
package LongAdder;
import java.util.concurrent.atomic.LongAdder;
public class AtomicIntegerTest implements Runnable {
LongAdder longAdder = new LongAdder();
public LongAdder getLongAdder() {
return longAdder;
}
public void setLongAdder(LongAdder longAdder) {
this.longAdder = longAdder;
}
@Override
public void run() {
longAdder.increment();
}
public static void main(String[] args) throws InterruptedException {
long start = System.currentTimeMillis();
AtomicIntegerTest test=new AtomicIntegerTest();
for (int i = 0; i < 100000; i++) {
Thread thread = new Thread(test::run);
thread.start();
thread.join();
}
Thread threadX = new Thread(new Runnable() {
public void run() {
System.out.println(test.getLongAdder());
long end = System.currentTimeMillis();
System.out.println((end-start)+"ms");
}
});
threadX.start();
}
}
版权声明
本文为[mabo_ [email protected]]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/200/202207170502057385.html
边栏推荐
猜你喜欢

第十届泰迪杯数据挖掘挑战赛A题害虫识别YOLOv5模型代码(已跑通,原创作品,持续更新)

MYSQL数据库表A数据同步到表B

上传七牛云的方法

ModerlArts第一次培训笔记

Simply and quickly establish a pytorch environment yolov5 target detection model to run (super simple)

CVE-2017-12635 Couchdb 垂直权限绕过漏洞复现

Restclient operation document

FanoutExchange交换机简单使用

3. Restclient query document

DirectExchange交换机的简单使用。
随机推荐
junit4和junit5区别
数据库取配置文件字段,然后进行数据处理和判断
Word2Vec原理及应用与文章相似度(推荐系统方法)
【C】张梁计算器
CVE-2020-10199 Nexus Repository Manager3远程命令执行漏洞复现
Attendance check-in and leave system based on SSM framework
One article to understand Zipkin
User management - restrictions
redis 安装
PyGame installation -requirement already satisfied
02_電影推薦(ContentBased)_用戶畫像
Add SSL certificate for load balancing
Learn about the configuration center
数据库实训7【索引与数据完整性约束的创建】
User management - paging
上传七牛云的方法
DSL查询文档
用户管理-分页
mysql数据库实验实训6,数据视图(详细)
swagger的坑