当前位置:网站首页>Pit encountered by handwritten ABA
Pit encountered by handwritten ABA
2022-07-06 22:26: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();Running results :

I found out A Thread switching back failed : At last, I got the idea ,Integer exceed 128 Meeting new, Instead of taking it from the cache , So it will fail .
Change to less than 128 Value , Observations :
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();
Find out aba The result was as successful as expected . You use Integer Be sure to note that the scope cannot exceed 128, Otherwise new object , Naturally, two objects are not the same .
边栏推荐
- PVL EDI 项目案例
- Seata aggregates at, TCC, Saga and XA transaction modes to create a one-stop distributed transaction solution
- Management background --4, delete classification
- 小程序系统更新提示,并强制小程序重启并使用新版本
- Management background --2 Classification list
- i.mx6ull搭建boa服务器详解及其中遇到的一些问题
- ZABBIX proxy server and ZABBIX SNMP monitoring
- Powerful domestic API management tool
- Heavyweight news | softing fg-200 has obtained China 3C explosion-proof certification to provide safety assurance for customers' on-site testing
- GPS from getting started to giving up (12), Doppler constant speed
猜你喜欢

将MySQL的表数据纯净方式导出

zabbix 代理服务器 与 zabbix-snmp 监控

Netxpert xg2 helps you solve the problem of "Cabling installation and maintenance"

Unity3d minigame unity webgl transform plug-in converts wechat games to use dlopen, you need to use embedded 's problem

Powerful domestic API management tool

Aardio - 通过变量名将变量值整合到一串文本中

Chapter 4: talk about class loader again
![[Digital IC hand tearing code] Verilog burr free clock switching circuit | topic | principle | design | simulation](/img/2b/15b3d831bba6aa772ad83f3ac91d23.png)
[Digital IC hand tearing code] Verilog burr free clock switching circuit | topic | principle | design | simulation

Aardio - 不声明直接传float数值的方法

Chapter 3: detailed explanation of class loading process (class life cycle)
随机推荐
自制J-Flash烧录工具——Qt调用jlinkARM.dll方式
pytorch_YOLOX剪枝【附代码】
Adjustable DC power supply based on LM317
China 1,4-cyclohexanedimethanol (CHDM) industry research and investment decision-making report (2022 Edition)
Assembly and Interface Technology Experiment 6 - ADDA conversion experiment, AD acquisition system in interrupt mode
2021 geometry deep learning master Michael Bronstein long article analysis
signed、unsigned关键字
Crawler obtains real estate data
Memorabilia of domestic database in June 2022 - ink Sky Wheel
[sdx62] wcn685x will bdwlan Bin and bdwlan Txt mutual conversion operation method
GD32F4XX串口接收中断和闲时中断配置
枚举与#define 宏的区别
软考高级(信息系统项目管理师)高频考点:项目质量管理
Aardio - 利用customPlus库+plus构造一个多按钮组件
2022-07-04 mysql的高性能数据库引擎stonedb在centos7.9编译及运行
每日一题:力扣:225:用队列实现栈
如何用程序确认当前系统的存储模式?
Management background --1 Create classification
中国1,4-环己烷二甲醇(CHDM)行业调研与投资决策报告(2022版)
硬件開發筆記(十): 硬件開發基本流程,制作一個USB轉RS232的模塊(九):創建CH340G/MAX232封裝庫sop-16並關聯原理圖元器件