当前位置:网站首页>CAS原子类型
CAS原子类型
2022-07-21 17:35:00 【卷王中王】
CAS(Compare and set/swap)比较和替换是设计并发算法时用到的一种技术。简单来说,比较和替换是使用一个期望值和一个变量的当前值进行比较,如果当前变量的值与我们期望的值相等,就使用一个新值替换当前变量的值。底层使用了unsafe类的一些方法,unsafe类中的操作可以实现类似c或c++那种直接操作内存,属于原语级别。
1. AtomicInteger
public static void main(String[] args) {
AtomicInteger ai = new AtomicInteger(); //默认值为0
AtomicInteger ai1 = new AtomicInteger(2); //默认值为2
int i = ai.get();//获取最新值
ai.incrementAndGet(); //i++
ai.getAndIncrement(); //++i
ai.decrementAndGet(); //--i
ai.getAndDecrement(); //i--
int next = i + 10;
ai.compareAndSet(i,next); //更新数值
ai.addAndGet(3); //i+3
ai.getAndAdd(3); //
ai.getAndUpdate(x->x*10); //i*10
}
2. AtomicBoolean
public static void main(String[] args) {
AtomicBoolean ab = new AtomicBoolean(false);
ab.compareAndSet(false,true);
ab.get();
ab.getAndSet(true);
ab.set(true);
}
3. AtomicLong
public static void main(String[] args) {
AtomicLong al = new AtomicLong();
long l = al.get();
al.incrementAndGet();
al.getAndIncrement();
al.decrementAndGet();
al.getAndDecrement();
al.addAndGet(10L);
al.getAndSet(10L);
al.getAndUpdate(x->x*10);
}
4. AtomicReference 原子引用
public static void main(String[] args) {
AtomicReference<String> ar = new AtomicReference<>("aaa");
ar.get();
ar.compareAndSet(ar.get(),"bbbb");
ar.getAndSet("cccc");
ar.set("ddddd");
ar.getAndUpdate(x->x+"aaa");
}
此方式不能察觉ABA问题,当值从A改为B又改为A,其他线程是无法察觉的。
5. AtomicStampedReference带有版本号的原子引用
此方式可以察觉ABA问题。
AtomicStampedReference<String> asr = new AtomicStampedReference<>("aaa",0);
asr.getReference();//获取当前值
asr.getStamp();//获取时间戳
asr.compareAndSet(asr.getReference(),"bbbbb",asr.getStamp(),2);
asr.set("cccc",3);
6. AtomicMarkableReference 带有修改标记的原子引用
AtomicMarkableReference<String> amr = new AtomicMarkableReference<>("aaaa",false);
amr.getReference();
amr.compareAndSet(amr.getReference(),"vvvv",false,true);
7. 原子数组
AtomicIntegerArray aia = new AtomicIntegerArray(3);
AtomicLongArray ala = new AtomicLongArray(3);
AtomicReferenceArray<String> ara = new AtomicReferenceArray<String>(3);
边栏推荐
- Writing of Samsung 6818 chip flame alarm driver
- Différence entre les versions Runtime + compilateur et Runtime only
- 使用style解决异形屏横屏时的白色边框
- 三星6818芯片火焰报警器驱动的编写
- QML drag pictures and objects across windows
- Ffmpeg audio decoding (seconds understand)
- 监督学习(回归、分类问题)与无监督学习(聚类问题)
- Solve the problem that the plug-in center of idea is not connected to the network
- 银河证券vip低佣金开户安全吗?是骗局吗?
- OSPF的路由控制
猜你喜欢
[C language] document operation "I"
Token
Cannot read property ‘type‘ of undefined Occurred while linting **\index. jsx:1
Postman configures the global variable postman sets the global token
力扣之滑动窗口《循序渐进》(209.长度最小的子数组、904. 水果成篮)
Pointer depth solution "three" (array cognition)
Pointer summary
E-commerce promotion relies on RPA to get rid of repeated work and rapidly improve efficiency
The foundation of writing notes in C language is not solid, and the earth is shaking
Document operation II (5000 word summary)
随机推荐
Hisilicon hi3531 | Ruixin micro rk1109 realizes H264 streaming with RTSP client
In quantitative trading, it is judged by the moving average system that the upward (downward) momentum is weakened
Commonly used instructions in RT thread
自定义View——点击冒泡效果
Conversion of JS time and timestamp
C#异常处理 | 连接成功但登陆异常/管道另一端上无任何进程Error: A connection was successfully established with the server, but
List、Set、Map的区别与联系
指针总结篇
js 判断数据是否为空
类的加载机制以及双亲委托机制
未配置在app.json的(uni发行微信小程序)
OSPF 重发布
[Samsung 6818] GPIO analog SPI signal writing access card identification module driver
js bind
文件操作《二》(5000字总结篇)
Quantitative transaction Diary - summary in February 2021
leetcode_两数相加_个人解法
管正雄:基于预训练模型、智能运维的QA生成算法落地
Try catch finally contains several cases of return and the return result
v-7