当前位置:网站首页>多线程学习笔记-1.CAS
多线程学习笔记-1.CAS
2022-07-26 17:56:00 【天生我才~~】
文章目录
慕课网多线程学习教程
1. 什么是CAS
1.1 简介
- 本质上是CPU的特殊指令
- 由于是指令,由CPU保证了原子性,因此是线程安全的


- 由于是指令,由CPU保证了原子性,因此是线程安全的
1.2. CAS的等价代码(语义)
/** * 描述: 模拟CAS操作,等价代码 */
public class SimulatedCAS {
private volatile int value;
// 整个方法对应一条指令==CAS指令
public synchronized int compareAndSwap(int expectedValue, int newValue) {
int oldValue = value;
if (oldValue == expectedValue) {
value = newValue;
}
return oldValue;
}
}
1.3 案例演示-模拟线程竞争
- 创建两个线程,同时执行模拟CAS操作
public class TwoThreadsCompetition implements Runnable {
private volatile int value;
public synchronized int compareAndSwap(int expectedValue, int newValue) {
int oldValue = value;
if (oldValue == expectedValue) {
value = newValue;
}
return oldValue;
}
// 模拟两个线程竞争
public static void main(String[] args) throws InterruptedException {
TwoThreadsCompetition r = new TwoThreadsCompetition();
r.value = 0;// 默认是期望值0
Thread t1 = new Thread(r,"Thread 1");
Thread t2 = new Thread(r,"Thread 2");
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println(r.value);
}
@Override
public void run() {
compareAndSwap(0, 1);
}
}
- 线程调试-开启多线程调试

- 先执行1线程-将value进行更改

- 再执行2线程,发现预期值与现有值不同,更新失败

3. 应用场景
3.1 乐观锁
3.2 并发容器
- 例如
ConcurrentHashMap
3.3 原子类

以AtomicInteger为例,分析CAS实现原理


volatile修饰
- 保证变量的可见性
Unsafe类

Unsafe类中的compareAndSwapInt

总结

5. 缺点和总结
5.1 ABA问题
- 如果只考虑值的变化,有可能A被别的线程改成B,最后又改成了A,这样,当前线程会误以为没有版本变化,这样后序的一些操作会有隐患
- 解决的策略是:仿照数据库,给每个版本增加一个版本号进行维护,每次比较不只看值,还比较版本号是否改变
5.2 自旋时间过长
- 如果竞争很激烈,锁一直拿不到,会一直自旋,会消耗CPU
边栏推荐
- 突发!Arm停止与华为合作!对华为影响究竟有多大?
- Accused of excessive patent licensing fees! The U.S. Court ruled that Qualcomm violated the antitrust law: Qualcomm's share price fell 10.86%!
- NFT digital collection development: digital collections help enterprise development
- Flask encapsulates seven cattle cloud
- Likeshop takeout order system is open source, 100% open source, no encryption
- FTP protocol
- Safer, healthier and without endurance anxiety, Wei brand latte dht-phev is here
- 如何做好测试用例设计
- 2022T电梯修理考试题及在线模拟考试
- FTP协议
猜你喜欢

Write a thesis and read this one

我酷故我在

Linked list - the first common node of two linked lists

SSM integration - functional module and interface testing

Pyqt5 rapid development and practice 3.5 menu bar and toolbar

The United States, Japan and South Korea jointly developed 6G with the intention of anti surpassing, but China has long been prepared

Have you ever encountered a deadlock problem in MySQL? How did you solve it?

14. Gradient detection, random initialization, neural network Summary

模板进阶(跑路人笔记)

NFT数字藏品开发:数字藏品助力企业发展
随机推荐
Paged query design of scenarios
Linked list - the first common node of two linked lists
自动化测试工具-Playwright(快速上手)
Flask encapsulates seven cattle cloud
OpenSSF 基金会总经理 Brian Behlendorf :预计 2026 年将有 4.2 亿个开源
测试组如何进行QA规范
Interface test scheme (interface test idea)
SSM integration - exception handler and project exception handling scheme
Flask 封装七牛云
Kubectl common commands and simple explanations
MES系统的选择需重点考虑哪些方面?
常用功能的测试用例
SSM整合-功能模块和接口测试
Likeshop takeout order system is open source, 100% open source, no encryption
更安全、更健康、无续航焦虑,魏牌拿铁DHT-PHEV来了
VPC nat (Sant, nant) experiment
2022年化工自动化控制仪表考题模拟考试平台操作
2022G1工业锅炉司炉上岗证题库及模拟考试
SSM整-整合配置
Neural network learning (2) introduction 2