当前位置:网站首页>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 .
边栏推荐
- 中国VOCs催化剂行业研究与投资战略报告(2022版)
- Crawler obtains real estate data
- Spatial domain and frequency domain image compression of images
- [线性代数] 1.3 n阶行列式
- 每日一题:力扣:225:用队列实现栈
- Chapter 3: detailed explanation of class loading process (class life cycle)
- 2500个常用中文字符 + 130常用中英文字符
- C#实现水晶报表绑定数据并实现打印4-条形码
- BarcodeX(ActiveX打印控件) v5.3.0.80 免费版使用
- void关键字
猜你喜欢
NetXpert XG2帮您解决“布线安装与维护”难题
Mysql database basic operations DML
2022-07-04 mysql的高性能数据库引擎stonedb在centos7.9编译及运行
C # realizes crystal report binding data and printing 4-bar code
Web APIs DOM 时间对象
小常识:保险中的“保全”是什么?
NPDP认证|产品经理如何跨职能/跨团队沟通?
2500个常用中文字符 + 130常用中英文字符
Embedded common computing artifact excel, welcome to recommend skills to keep the document constantly updated and provide convenience for others
PVL EDI project case
随机推荐
Data storage (1)
Adjustable DC power supply based on LM317
labelimg的安装与使用
[MySQL] online DDL details
i.mx6ull搭建boa服务器详解及其中遇到的一些问题
C#實現水晶報錶綁定數據並實現打印4-條形碼
Management background --4, delete classification
2022-07-05 stonedb的子查询处理解析耗时分析
微信红包封面小程序源码-后台独立版-带测评积分功能源码
Daily question 1: force deduction: 225: realize stack with queue
[leetcode daily clock in] 1020 Number of enclaves
Four data streams of grpc
NetXpert XG2帮您解决“布线安装与维护”难题
[sciter bug] multi line hiding
Oracle-控制文件及日志文件的管理
LeetCode刷题(十一)——顺序刷题51至55
PVL EDI 项目案例
China 1,4-cyclohexanedimethanol (CHDM) industry research and investment decision-making report (2022 Edition)
[10:00 public class]: basis and practice of video quality evaluation
Solve project cross domain problems