当前位置:网站首页>手写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 对象,自然两个对象不是同一个。
边栏推荐
- 414. The third largest digital buckle
- 第3章:类的加载过程(类的生命周期)详解
- GPS from getting started to giving up (XIII), receiver autonomous integrity monitoring (RAIM)
- GD32F4XX串口接收中断和闲时中断配置
- C # réalise la liaison des données du rapport Crystal et l'impression du Code à barres 4
- 小常识:保险中的“保全”是什么?
- Oracle-控制文件及日志文件的管理
- Management background --3, modify classification
- 嵌入式常用计算神器EXCEL,欢迎各位推荐技巧,以保持文档持续更新,为其他人提供便利
- GNN, please deepen your network layer~
猜你喜欢
C#實現水晶報錶綁定數據並實現打印4-條形碼
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
GPS从入门到放弃(十五)、DCB差分码偏差
C # réalise la liaison des données du rapport Crystal et l'impression du Code à barres 4
Crawler obtains real estate data
Data processing skills (7): MATLAB reads the data in the text file TXT with mixed digital strings
HDR image reconstruction from a single exposure using deep CNN reading notes
第3章:类的加载过程(类的生命周期)详解
2022-07-04 mysql的高性能数据库引擎stonedb在centos7.9编译及运行
Shell product written examination related
随机推荐
Anaconda installs third-party packages
GPS from getting started to giving up (XI), differential GPS
经纪xx系统节点VIP案例介绍和深入分析异常
HDR image reconstruction from a single exposure using deep CNNs阅读札记
GPS从入门到放弃(十五)、DCB差分码偏差
Unity3D学习笔记6——GPU实例化(1)
i.mx6ull搭建boa服务器详解及其中遇到的一些问题
Assembly and Interface Technology Experiment 6 - ADDA conversion experiment, AD acquisition system in interrupt mode
CCNA Cisco network EIGRP protocol
[leetcode daily clock in] 1020 Number of enclaves
GPS from getting started to giving up (19), precise ephemeris (SP3 format)
Management background --5, sub classification
Management background --4, delete classification
小程序系统更新提示,并强制小程序重启并使用新版本
Assembly and interface technology experiment 5-8259 interrupt experiment
414. The third largest digital buckle
[10:00 public class]: basis and practice of video quality evaluation
GPS from entry to abandonment (XVII), tropospheric delay
Classic sql50 questions
【数字IC手撕代码】Verilog无毛刺时钟切换电路|题目|原理|设计|仿真