当前位置:网站首页>ABA问题遇到过吗,详细说以下,如何避免ABA问题
ABA问题遇到过吗,详细说以下,如何避免ABA问题
2022-07-06 09:20:00 【快醒醒鸭今天你编程了吗?】
1、有两个线程同时修改一个变量的值,线程1、线程2,都更新变量值,将变量从A更新为B
2、首先线程1获得到CPU时间片,线程2由于某些原因原因发生等待,线程1进行比较更新,CAS
(CompareAndSwap),成功将变量的值从A更新到B
3、更新完毕后,恰好又有线程3进来想要把变量的值从B更新到A,线程3进行比较更新,成功将变量的值从B更新为A
4、线程2获得到CPU时间片,然后进行比较更新,发现值是预期的A,然后又更新成了B,但是线程1并不知道该值已经从A-->B-->A这个过程,这就是ABA问题。
如何避免ABA问题?
可以通过加版本号或者时间戳解决,或者保证单向递增或递减就不会存在此类问题,atomic包下的AtomicStampedReference类的CompareAndSet方法首先检查当前引用是否等于预期引用,并且当前标志是否等于预期标志,如果全部相等,则以原子方式将该标志的值设置给定的更新值
package com.ws.cas;
import java.util.concurrent.atomic.AtomicInteger;
public class CASDemo {
//CAS compareAndSet:比较并交换!!!
public static void main(String[] args) {
AtomicInteger atomicInteger = new AtomicInteger(2020);
//对于我们平时写的SQL来说:乐观锁!
//第一个参数是期望、第二个参数是更新
//public final boolean compareAndSet(int expect, int update)
//如果我期望的值达到了,那么就更新,否则就不更新 CAS是CPU的并发原语
//乱搞线程
System.out.println(atomicInteger.compareAndSet(2020, 2021));
System.out.println(atomicInteger.get());
System.out.println(atomicInteger.compareAndSet(2021, 2020));
System.out.println(atomicInteger.get());
//期望的线程
System.out.println(atomicInteger.compareAndSet(2020, 6666));
System.out.println(atomicInteger.get());
}
}
CAS导致的其他问题?
- 只能保证一个共享变量的原子操作
- 自旋CAS如果长时间不成功,会给CPU带来开销大的问题
边栏推荐
- 继承和多态(上)
- 初识C语言(下)
- MySQL 30000 word essence summary + 100 interview questions, hanging the interviewer is more than enough (Collection Series
- 165. Compare version number - string
- E-R graph to relational model of the 2022 database of tyut Taiyuan University of Technology
- 最新坦克大战2022-全程开发笔记-3
- 5.MSDN的下载和使用
- 【毕业季·进击的技术er】再见了,我的学生时代
- 5. Download and use of MSDN
- 初识指针笔记
猜你喜欢
Differences and application scenarios between MySQL index clock B-tree, b+tree and hash indexes
MySQL 30000 word essence summary + 100 interview questions, hanging the interviewer is more than enough (Collection Series
8. C language - bit operator and displacement operator
Rich Shenzhen people and renting Shenzhen people
TYUT太原理工大学2022数据库大题之分解关系模式
3.输入和输出函数(printf、scanf、getchar和putchar)
TYUT太原理工大学2022数据库之关系代数小题
西安电子科技大学22学年上学期《基础实验》试题及答案
System design learning (III) design Amazon's sales rank by category feature
1.C语言矩阵加减法
随机推荐
1.C语言初阶练习题(1)
[Topic terminator]
vector
(ultra detailed onenet TCP protocol access) arduino+esp8266-01s access to the Internet of things platform, upload real-time data collection /tcp transparent transmission (and how to obtain and write L
6. Function recursion
Common method signatures and meanings of Iterable, collection and list
12 excel charts and arrays
C语言入门指南
魏牌:产品叫好声一片,但为何销量还是受挫
Alibaba cloud microservices (II) distributed service configuration center and Nacos usage scenarios and implementation introduction
6.函数的递归
The overseas sales of Xiaomi mobile phones are nearly 140million, which may explain why Xiaomi ov doesn't need Hongmeng
JS interview questions (I)
初识指针笔记
Redis cache obsolescence strategy
C语言实现扫雷游戏(完整版)
Share a website to improve your Aesthetics
继承和多态(下)
C language to achieve mine sweeping game (full version)
162. Find peak - binary search