当前位置:网站首页>手写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 对象,自然两个对象不是同一个。
边栏推荐
- [leetcode daily clock in] 1020 Number of enclaves
- 【数字IC手撕代码】Verilog无毛刺时钟切换电路|题目|原理|设计|仿真
- CCNA-思科网络 EIGRP协议
- Mise en place d'un environnement de développement OP - tee basé sur qemuv8
- Management background --1 Create classification
- VIP case introduction and in-depth analysis of brokerage XX system node exceptions
- AI enterprise multi cloud storage architecture practice | Shenzhen potential technology sharing
- qt quick项目offscreen模式下崩溃的问题处理
- NetXpert XG2帮您解决“布线安装与维护”难题
- GPS从入门到放弃(十八)、多路径效应
猜你喜欢
随机推荐
GPS从入门到放弃(十八)、多路径效应
Codeforces Round #274 (Div. 2) –A Expression
墨西哥一架飞往美国的客机起飞后遭雷击 随后安全返航
Leetcode question brushing (XI) -- sequential questions brushing 51 to 55
AI enterprise multi cloud storage architecture practice | Shenzhen potential technology sharing
3DMax指定面贴图
[Digital IC hand tearing code] Verilog burr free clock switching circuit | topic | principle | design | simulation
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
Kohana 数据库
Management background --3, modify classification
Save and retrieve strings
GNN, please deepen your network layer~
A Mexican airliner bound for the United States was struck by lightning after taking off and then returned safely
C # réalise la liaison des données du rapport Crystal et l'impression du Code à barres 4
GNN,请你的网络层数再深一点~
qt quick项目offscreen模式下崩溃的问题处理
Attack and defense world ditf Misc
Assembly and interface technology experiment 5-8259 interrupt experiment
Lora sync word settings
GPS从入门到放弃(十七) 、对流层延时








