当前位置:网站首页>手写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 对象,自然两个对象不是同一个。
边栏推荐
猜你喜欢

Mise en place d'un environnement de développement OP - tee basé sur qemuv8

Classic sql50 questions

Unity3d Learning Notes 6 - GPU instantiation (1)

Oracle-控制文件及日志文件的管理

2022年6月国产数据库大事记-墨天轮

GNN,请你的网络层数再深一点~

GPS从入门到放弃(十三)、接收机自主完好性监测(RAIM)

小常识:保险中的“保全”是什么?

C#實現水晶報錶綁定數據並實現打印4-條形碼

Assembly and interface technology experiment 5-8259 interrupt experiment
随机推荐
AI enterprise multi cloud storage architecture practice | Shenzhen potential technology sharing
Lora sync word settings
Anaconda installs third-party packages
中国固态氧化物燃料电池技术进展与发展前景报告(2022版)
About the professional ethics of programmers, let's talk about it from the way of craftsmanship and neatness
GPS from getting started to giving up (16), satellite clock error and satellite ephemeris error
Oracle control file and log file management
Force buckle 575 Divide candy
GPS从入门到放弃(二十)、天线偏移
Oracle-控制文件及日志文件的管理
HDR image reconstruction from a single exposure using deep CNN reading notes
The nearest common ancestor of binary (search) tree ●●
GPS from getting started to giving up (19), precise ephemeris (SP3 format)
Codeforces Round #274 (Div. 2) –A Expression
[线性代数] 1.3 n阶行列式
Unity3d Learning Notes 6 - GPU instantiation (1)
[leetcode daily clock in] 1020 Number of enclaves
嵌入式常用计算神器EXCEL,欢迎各位推荐技巧,以保持文档持续更新,为其他人提供便利
中国VOCs催化剂行业研究与投资战略报告(2022版)
Insert sort and Hill sort