当前位置:网站首页>手写ABA遇到的坑
手写ABA遇到的坑
2022-07-06 14:31:00 【Hide on jdk】
AtomicStampedReference<Integer> stampedReference = new AtomicStampedReference<>(100, 1);
Thread t1 = new Thread(()->{
boolean b = stampedReference.compareAndSet(100, 200, stampedReference.getStamp(), stampedReference.getStamp()+1);
System.out.println("t1 "+b);
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
boolean b1 = stampedReference.compareAndSet(200, 100, stampedReference.getStamp(), stampedReference.getStamp()+1);
System.out.println("t1 "+b1+" "+ stampedReference.getReference()+" "+stampedReference.getStamp());
},"t1");
t1.start();
Thread t2 = new Thread(()->{
System.out.println(stampedReference.getReference()+" "+stampedReference.getStamp());
boolean b = stampedReference.compareAndSet(200, 300, stampedReference.getStamp(), stampedReference.getStamp()+1);
System.out.println("t2 "+b);
boolean c = stampedReference.compareAndSet(300, 200, stampedReference.getStamp(), stampedReference.getStamp()+1);
System.out.println("t2 "+c);
},"t2");
t2.start();运行结果:

居然发现A线程换回去失败了:最后终于想明白了,Integer超过128会new,而不是从缓存中拿,所以会比较失败。
修改成小于128的值,观察结果:
Thread t1 = new Thread(()->{
boolean b = stampedReference.compareAndSet(100, 105, stampedReference.getStamp(), stampedReference.getStamp()+1);
System.out.println("t1 "+b);
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
boolean b1 = stampedReference.compareAndSet(105, 100, stampedReference.getStamp(), stampedReference.getStamp()+1);
System.out.println("t1 "+b1+" "+ stampedReference.getReference()+" "+stampedReference.getStamp());
},"t1");
t1.start();
Thread t2 = new Thread(()->{
System.out.println(stampedReference.getReference()+" "+stampedReference.getStamp());
boolean b = stampedReference.compareAndSet(105, 104, stampedReference.getStamp(), stampedReference.getStamp()+1);
System.out.println("t2 "+b);
boolean c = stampedReference.compareAndSet(104, 105, stampedReference.getStamp(), stampedReference.getStamp()+1);
System.out.println("t2 "+c);
},"t2");
t2.start();
发现aba结果成功和预期的一样。大家使用Integer的时候一定要注意范围不能超过128,否则会new 对象,自然两个对象不是同一个。
边栏推荐
- 做接口测试都测什么?有哪些通用测试点?
- 2022-07-05 stonedb的子查询处理解析耗时分析
- 微信红包封面小程序源码-后台独立版-带测评积分功能源码
- 2500 common Chinese characters + 130 common Chinese and English characters
- Assembly and interface technology experiment 5-8259 interrupt experiment
- 【sciter Bug篇】多行隐藏
- GPS from getting started to giving up (XVIII), multipath effect
- 2020 Bioinformatics | GraphDTA: predicting drug target binding affinity with graph neural networks
- Chapter 3: detailed explanation of class loading process (class life cycle)
- Xiaoman network model & http1-http2 & browser cache
猜你喜欢

GPS从入门到放弃(十七) 、对流层延时
![[线性代数] 1.3 n阶行列式](/img/6e/54f3a994fc4c2c10c1036bee6715e8.gif)
[线性代数] 1.3 n阶行列式

UNI-Admin基础框架怎么关闭创建超级管理员入口?

BarcodeX(ActiveX打印控件) v5.3.0.80 免费版使用

Adjustable DC power supply based on LM317

PVL EDI project case

labelimg的安装与使用

GPS from getting started to giving up (XV), DCB differential code deviation

About the professional ethics of programmers, let's talk about it from the way of craftsmanship and neatness

微信红包封面小程序源码-后台独立版-带测评积分功能源码
随机推荐
C # réalise la liaison des données du rapport Crystal et l'impression du Code à barres 4
GPS从入门到放弃(十一)、差分GPS
解决项目跨域问题
嵌入式常用计算神器EXCEL,欢迎各位推荐技巧,以保持文档持续更新,为其他人提供便利
重磅新闻 | Softing FG-200获得中国3C防爆认证 为客户现场测试提供安全保障
0 basic learning C language - interrupt
二分图判定
Xiaoman network model & http1-http2 & browser cache
The nearest common ancestor of binary (search) tree ●●
CCNA-思科网络 EIGRP协议
[leetcode daily clock in] 1020 Number of enclaves
Assembly and interface technology experiment 5-8259 interrupt experiment
保存和检索字符串
Oracle-控制文件及日志文件的管理
【sdx62】WCN685X将bdwlan.bin和bdwlan.txt相互转化操作方法
[sdx62] wcn685x will bdwlan Bin and bdwlan Txt mutual conversion operation method
Seata聚合 AT、TCC、SAGA 、 XA事务模式打造一站式的分布式事务解决方案
Data processing skills (7): MATLAB reads the data in the text file TXT with mixed digital strings
Report on technological progress and development prospects of solid oxide fuel cells in China (2022 Edition)
数据处理技巧(7):MATLAB 读取数字字符串混杂的文本文件txt中的数据