当前位置:网站首页>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;
}
}
边栏推荐
- Redis高可用——主从复制、哨兵模式、集群
- 698. 划分为k个相等的子集 ●●
- AsyncSocket长连接棒包装问题解决
- Spire.PDF for NET 8.7.2
- 4 points tell you the advantages of the combination of real-time chat and chat robots
- 【LeetCode】5. Valid palindrome
- Fiddler Everywhere 3.2.1 Crack
- 4点告诉你实时聊天与聊天机器人组合的优势
- Spire Office 7.5.4 for NET
- Creative mode 1 - single case mode
猜你喜欢

Spreadjs 15.1 CN and spreadjs 15.1 en

GFS Distributed File System

Spire. PDF for NET 8.7.2

Go language implementation principle -- map implementation principle

Data analysis - Thinking foreshadowing

TVS管和ESD管的技術指標和選型指南-嘉立創推薦

2: Chapter 1: understanding JVM specification 1: introduction to JVM;

Fiddler Everywhere 3.2.1 Crack

GFS分布式文件系統

Mathematical formula screenshot recognition artifact mathpix unlimited use tutorial
随机推荐
MySQL (1) -- related concepts, SQL classification, and simple operations
C# 反射与Type
When to use useImperativeHandle, useLayoutEffect, and useDebugValue
Brushless drive design -- on MOS drive circuit
Switching power supply buck circuit CCM and DCM working mode
poj 2762 Going from u to v or from v to u? (推断它是否是一个薄弱环节图)
Do you regret becoming a programmer?
Live tiktok shop 2022 latest gameplay card slot overseas live e-commerce new traffic
如何让同步/刷新的图标(el-icon-refresh)旋转起来
MySQL delete uniqueness constraint unique
Dynamic planning: robbing families and houses
Data analysis - Thinking foreshadowing
Rsync remote synchronization
【经典控制理论】自控实验总结
5. Logistic regression
Difference between out of band and in band
证明 poj 1014 模优化修剪,部分递归 有错误
如何获取localStorage中存储的所有值
golang代码检查工具
CIS基准测试工具kube-bench使用