当前位置:网站首页>手写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 对象,自然两个对象不是同一个。
边栏推荐
- 保存和检索字符串
- i. Mx6ull build boa server details and some of the problems encountered
- CCNA-思科网络 EIGRP协议
- QT | UDP broadcast communication, simple use case
- Attack and defense world miscall
- Powerful domestic API management tool
- Barcodex (ActiveX print control) v5.3.0.80 free version
- 中国固态氧化物燃料电池技术进展与发展前景报告(2022版)
- qt quick项目offscreen模式下崩溃的问题处理
- Inno Setup 打包及签名指南
猜你喜欢
Management background --2 Classification list
About the professional ethics of programmers, let's talk about it from the way of craftsmanship and neatness
Management background --5, sub classification
GPS从入门到放弃(十九)、精密星历(sp3格式)
Notes de développement du matériel (10): flux de base du développement du matériel, fabrication d'un module USB à RS232 (9): création de la Bibliothèque d'emballage ch340g / max232 SOP - 16 et Associa
The nearest common ancestor of binary (search) tree ●●
HDR image reconstruction from a single exposure using deep CNNs阅读札记
BarcodeX(ActiveX打印控件) v5.3.0.80 免费版使用
NetXpert XG2帮您解决“布线安装与维护”难题
RESNET rs: Google takes the lead in tuning RESNET, and its performance comprehensively surpasses efficientnet series | 2021 arXiv
随机推荐
Hardware development notes (10): basic process of hardware development, making a USB to RS232 module (9): create ch340g/max232 package library sop-16 and associate principle primitive devices
i.mx6ull搭建boa服务器详解及其中遇到的一些问题
414. The third largest digital buckle
Kohana database
Chapter 4: talk about class loader again
Unity3d Learning Notes 6 - GPU instantiation (1)
Force deduction question 500, keyboard line, JS implementation
China 1,4-cyclohexanedimethanol (CHDM) industry research and investment decision-making report (2022 Edition)
0 basic learning C language - digital tube
硬件開發筆記(十): 硬件開發基本流程,制作一個USB轉RS232的模塊(九):創建CH340G/MAX232封裝庫sop-16並關聯原理圖元器件
Support multiple API versions in flask
UNI-Admin基础框架怎么关闭创建超级管理员入口?
C#实现水晶报表绑定数据并实现打印4-条形码
GNN,请你的网络层数再深一点~
Set status bar style demo
[线性代数] 1.3 n阶行列式
2020 Bioinformatics | GraphDTA: predicting drug target binding affinity with graph neural networks
Report on technological progress and development prospects of solid oxide fuel cells in China (2022 Edition)
Classic sql50 questions
How does the uni admin basic framework close the creation of super administrator entries?