当前位置:网站首页>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
边栏推荐
- CVE-2021-44228 Log4j 复现及原理
- First training notes of moderlarts
- Microservice high concurrency service governance
- Notes de formation pour la deuxième fois des modèles
- 决策树原理和案例应用-泰坦尼克号生存预测
- 机器学习之特征提取(类别特征进行数值化、离散化、文本特征进行数值化)
- Cve-2019-14234 Django jsonfield SQL injection vulnerability
- 第十届泰迪杯数据挖掘挑战赛A题害虫识别YOLOv5模型代码(已跑通,原创作品,持续更新)
- RestAPI
- Shallow chat link tracking
猜你喜欢

User management - restrictions

SQL语句学习

日志加入数据库实现思路

数据分析与数据挖掘实战案例本地房价预测(716):

Topicexchange switch is simple to use.

基于cuda10.0的pytorch深度学习环境配置

HarmonyOS第二次培训笔记

简单快速建立pytorch环境YOLOv5目标检测 模型跑起来(超简单)
[email protected] : `node install. Problems of js`"/>solve [email protected] : `node install. Problems of js`

Elment UI usage
随机推荐
MYSQL模糊匹配1,11,111这种相似字符串问题
ES文档操作
How to upload qiniu cloud
Learn about scheduled tasks in one article
Attendance check-in and leave system based on SSM framework
微众对接机制备忘
【Batch】批量删除中间文件夹-个人研究脚本
One article to understand Zipkin
数据可视化
CVE-2017-12635 Couchdb 垂直权限绕过漏洞复现
DSL query document
ModelArts第二次培訓筆記
Logic of image uploading
Word2Vec原理及应用与文章相似度(推荐系统方法)
Pygame:外星人入侵
机器学习之特征提取(类别特征进行数值化、离散化、文本特征进行数值化)
加密和解密
POC——DVWA‘s SQL Injection
Simply and quickly establish a pytorch environment yolov5 target detection model to run (super simple)
SQL statement learning