当前位置:网站首页>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
边栏推荐
- Restclient operation document
- One article to understand Zipkin
- Cve-2019-14234 Django jsonfield SQL injection vulnerability
- 关于当前响应已经调用了方法getOutputStream()
- Yiwen takes you to know about haproxy
- 用户登录-以及创建验短信证码
- Feature extraction of machine learning (digitization and discretization of category features and digitization of text features)
- Notes de formation pour la deuxième fois des modèles
- The database takes the fields of the configuration file, and then processes and judges the data
- Install MySQL
猜你喜欢

ModelArts第二次培训笔记

POC——DVWA‘s SQL Injection

SMS verification test without signature template audit

NVIDIA GeForce Experience登录报错:验证程序加载失败,请检查您的浏览器设置,例如广告拦截程序(解决办法)

Harmonyos fourth training notes

PyGame installation -requirement already satisfied

MYSQL模糊匹配1,11,111这种相似字符串问题

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

浅聊链路追踪

About the current response, the method getoutputstream() has been called
随机推荐
一文带你了解HAProxy
CVE-2020-10199 Nexus Repository Manager3远程命令执行漏洞复现
MYSQL两个查询条件取并集然后进行查询
02_ Movie recommendation (contentbased)_ User portrait
数据库取配置文件字段,然后进行数据处理和判断
User management - paging
TopicExchange交换机简单使用。
DirectExchange交换机的简单使用。
. SH scripting
The database takes the fields of the configuration file, and then processes and judges the data
Use of flask
NoSQL概述
Microservice high concurrency service governance
Harmonyos second training notes
CVE-2021-44228 Log4j 复现及原理
Harmonyos third training notes
NVIDIA GeForce Experience登录报错:验证程序加载失败,请检查您的浏览器设置,例如广告拦截程序(解决办法)
一文了解配置中心
浅聊链路追踪
User - registration / login