对CAS中atomicInteger实现的思考
2024-09-06 02:13:56
p.p1 { margin: 0; font: 11px Monaco }
span.s1 { color: rgba(147, 26, 104, 1) }
span.s2 { color: rgba(126, 80, 79, 1) }
CAS是unSafe类中compareAndSwapInt,compareAndSwapLong等几个方法包装提供
public final int incrementAndGet() {
for (;;) {
int current = get();
int next = current + 1;
if (compareAndSet(current, next))
return next;
}
}
p.p1 { margin: 0; font: 11px Monaco }
span.s1 { color: rgba(147, 26, 104, 1) }
span.s2 { color: rgba(126, 80, 79, 1) }
span.s3 { color: rgba(3, 38, 204, 1) }
public final boolean compareAndSet(int expect, int update) {
return unsafe.compareAndSwapInt(this, valueOffset, expect, update);
}
原来对它的原理一直不大理解
通过注释解读反而很清楚了
原来它是通过比较value和上文中的所谓的期望值current进行最终比较 如果相等 认为没被篡改过
但其实还是会有ABA问题
就是
最新文章
- mac 终端 常用命令
- Yii2 用户登录
- Button 设置适应不同版本 旋转以后大小相应的改变
- zepto - push
- 奇怪的JS正则之 /[A-z]/.test(";\\";); // true
- UltraEdit 和Notepad++ 使用ftp直接编辑linux上文件
- poj 2094 多项式求和。
- hdu1114(完全背包)
- clientIDMode的应用
- win7 Python 环境 准备 配置
- 随笔css的样式
- (一)php的基本知识和一些注意点
- 3Delight NSI: A Streamable Render API
- day16
- Web 开发技术文档大全
- Django框架(三)
- linux命令行命令
- 用TexturePacker打图集用于UGUI中
- datatable表格框架服务器端分页查询设置
- 使用java8的StreamAPI对集合计算进行代码重构
热门文章
- Python使用import导入模块时报ValueError: source code string cannot contain null bytes的解决方案
- 第8章 Python类中常用的特殊变量和方法目录
- PyQt(Python+Qt)学习随笔:gridLayout的layoutHorizontalSpacing和layoutVerticalSpacing属性
- Java数据结构(十五)—— 多路查找树
- 安装nginx并安全地配置和启动
- Echarts的食用方式
- Python-Wechaty: 面向所有IM软件的聊天机器人框架
- 题解-洛谷P7114 字符串匹配
- C# 高性能对象映射
- sqli-labs lexx25-28a(各种过滤)