当前位置:网站首页>手写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 对象,自然两个对象不是同一个。
边栏推荐
- GPS from getting started to giving up (16), satellite clock error and satellite ephemeris error
- Mise en place d'un environnement de développement OP - tee basé sur qemuv8
- 0 basic learning C language - interrupt
- Unity3d minigame-unity-webgl-transform插件转换微信小游戏报错To use dlopen, you need to use Emscripten‘s...问题
- Spatial domain and frequency domain image compression of images
- Insert sort and Hill sort
- GPS from getting started to giving up (XX), antenna offset
- NetXpert XG2帮您解决“布线安装与维护”难题
- 中国VOCs催化剂行业研究与投资战略报告(2022版)
- AI enterprise multi cloud storage architecture practice | Shenzhen potential technology sharing
猜你喜欢
Spatial domain and frequency domain image compression of images
Barcodex (ActiveX print control) v5.3.0.80 free version
GPS从入门到放弃(十七) 、对流层延时
Management background --3, modify classification
Management background --2 Classification list
Data processing skills (7): MATLAB reads the data in the text file TXT with mixed digital strings
Mise en place d'un environnement de développement OP - tee basé sur qemuv8
GNN, please deepen your network layer~
2020 Bioinformatics | GraphDTA: predicting drug target binding affinity with graph neural networks
Management background --4, delete classification
随机推荐
anaconda安装第三方包
2020 Bioinformatics | GraphDTA: predicting drug target binding affinity with graph neural networks
Support multiple API versions in flask
HDR image reconstruction from a single exposure using deep CNN reading notes
China 1,4-cyclohexanedimethanol (CHDM) industry research and investment decision-making report (2022 Edition)
GNN,请你的网络层数再深一点~
Research and investment strategy report of China's VOCs catalyst industry (2022 Edition)
Codeforces Round #274 (Div. 2) –A Expression
Mongodb (III) - CRUD
第3章:类的加载过程(类的生命周期)详解
解决项目跨域问题
Powerful domestic API management tool
Insert sort and Hill sort
GPS从入门到放弃(十二)、 多普勒定速
二分图判定
墨西哥一架飞往美国的客机起飞后遭雷击 随后安全返航
zabbix 代理服务器 与 zabbix-snmp 监控
ResNet-RS:谷歌领衔调优ResNet,性能全面超越EfficientNet系列 | 2021 arxiv
GPS从入门到放弃(十四)、电离层延时
Shell product written examination related