当前位置:网站首页>Use CAS instead of synchronized
Use CAS instead of synchronized
2022-07-05 23:39:00 【Archie_ java】
It needs to be often used in development synchronized Guarantee code Thread safety , Waiting resources will be blocked under competitive conditions , If you are allowed to compete for less than resources, return failure , You can use cas Reduce blocking time . Let's have a look at cas Single instance mode of .
public class NonBlock {
private static volatile NonBlock nonBlock;
private static AtomicBoolean atomicBoolean = new AtomicBoolean(false);
public static NonBlock getInstance() {
if (nonBlock == null) {
if (atomicBoolean.compareAndSet(false, true)) {
nonBlock = new NonBlock();
}
}
return nonBlock;
}
}
In this singleton mode , differ synchronized The block , Multi-threaded environment ,getInstance Ensure that only one object will be created , Possible return nonBlock Is an empty object . but , If it is allowed to return an empty object ,
public class RedPacket {
private long balance;
private int num;
public RedPacket(long balance, int num) {
this.balance = balance;
this.num = num;
}
public long get() {
if (balance < 1 || num < 1) {
return -1;
}
if (num == 1) {
long result = balance;
balance = 0;
num--;
return result;
}
long average = balance / num;
long result = ThreadLocalRandom.current().nextLong(1, average * 2);
balance -= result;
num--;
return result;
}
We can also use cas Achieve the purpose of non blocking , This ensures thread safety , When there is competition, it indicates that the competition has failed , The exact point is to prompt that the balance may be greater than 0, Coming first may not be able to grab , Later, people can rob .
public class RedPacket {
private long balance;
private AtomicInteger num;
public RedPacket(long balance, int num) {
this.balance = balance;
this.num = new AtomicInteger(num);
}
public long get() {
int number = num.get();
long balan = balance;
if (balan < 1 || number < 1) {
return -1;
}
if (num.compareAndSet(number, number - 1)) {
if (number - 1 == 0) {
balance = 0;
return balan;
}
long average = balan / number;
long result = ThreadLocalRandom.current().nextLong(1, average * 2);
balance -= result;
}
return -1;
}
}
边栏推荐
- Introduction to JVM
- 哪些偏门项目可以做到?自媒体做到月赚一万以上很难吗?
- el-cascader的使用以及报错解决
- AsyncSocket长连接棒包装问题解决
- 《牛客刷verilog》Part III Verilog企业真题
- Spécifications techniques et lignes directrices pour la sélection des tubes TVS et ESD - Recommandation de jialichuang
- Rasa 3. X learning series -rasa x Community Edition (Free Edition) changes
- Pyqt control part (I)
- TVS管和ESD管的技术指标和选型指南-嘉立创推荐
- 【原创】程序员团队管理的核心是什么?
猜你喜欢
Rasa 3.x 学习系列-Rasa 3.2.1 新版本发布
Technical specifications and model selection guidelines for TVs tubes and ESD tubes - recommended by jialichuang
【LeetCode】5. Valid Palindrome·有效回文
CIS基准测试工具kube-bench使用
el-cascader的使用以及报错解决
Neural structured learning - Part 2: training with natural graphs
3: Chapter 1: understanding JVM specification 2: JVM specification, introduction;
如何获取localStorage中存储的所有值
rsync远程同步
How to design API return code (error code)?
随机推荐
证明 poj 1014 模优化修剪,部分递归 有错误
Dynamic planning: robbing families and houses
Development specification: interface unified return value format [resend]
Brushless drive design -- on MOS drive circuit
Rasa 3.x 学习系列-Rasa X 社区版(免费版) 更改
MySQL replace primary key delete primary key add primary key
Summary of binary tree recursive routines
Neural structured learning - Part 2: training with natural graphs
Rasa 3.x 学习系列-Rasa 3.2.1 新版本发布
代码农民提高生产力
Neural structured learning - Part 3: training with synthesized graphs
2: Chapter 1: understanding JVM specification 1: introduction to JVM;
《牛客刷verilog》Part III Verilog企业真题
idea 连接mysql ,直接贴配置文件的url 比较方便
GFS分布式文件系統
Hcip course notes-16 VLAN, three-tier architecture, MPLS virtual private line configuration
Scala concurrent programming (II) akka
C# 文件与文件夹操作
poj 2762 Going from u to v or from v to u? (推断它是否是一个薄弱环节图)
Go language introduction detailed tutorial (I): go language in the era