当前位置:网站首页>Have you encountered ABA problems? Let's talk about the following in detail, how to avoid ABA problems
Have you encountered ABA problems? Let's talk about the following in detail, how to avoid ABA problems
2022-07-06 13:39:00 【Wake up duck, did you program today?】
1、 There are two threads modifying the value of a variable at the same time , Threads 1、 Threads 2, Update variable values , Change variables from A Updated to B
2、 The first thread 1 To obtain CPU Time slice , Threads 2 Wait for some reason , Threads 1 Compare and update ,CAS
(CompareAndSwap), Successfully changed the value of the variable from A Update to B
3、 After updating , There happens to be another thread 3 Come in and want to change the value of the variable from B Update to A, Threads 3 Compare and update , Successfully changed the value of the variable from B Updated to A
4、 Threads 2 To obtain CPU Time slice , Then compare and update , The value found is expected A, And then it's updated B, But threads 1 I don't know that the value has changed from A-->B-->A This process , This is it. ABA problem .
How to avoid ABA problem ?
It can be solved by adding version number or timestamp , Or guarantee one-way increasing or decreasing, there will be no such problems ,atomic Under bag AtomicStampedReference Class CompareAndSet Method first checks whether the current reference is equal to the expected reference , And whether the current flag is equal to the expected flag , If all are equal , Then set the value of this flag to the given update value in an atomic way
package com.ws.cas;
import java.util.concurrent.atomic.AtomicInteger;
public class CASDemo {
//CAS compareAndSet: Compare and exchange !!!
public static void main(String[] args) {
AtomicInteger atomicInteger = new AtomicInteger(2020);
// For what we usually write SQL Come on : Optimism lock !
// The first parameter is expectation 、 The second parameter is update
//public final boolean compareAndSet(int expect, int update)
// If my expectations reach , Then update , Otherwise, it will not be updated CAS yes CPU The concurrent primitives of
// Mess with threads
System.out.println(atomicInteger.compareAndSet(2020, 2021));
System.out.println(atomicInteger.get());
System.out.println(atomicInteger.compareAndSet(2021, 2020));
System.out.println(atomicInteger.get());
// Expected thread
System.out.println(atomicInteger.compareAndSet(2020, 6666));
System.out.println(atomicInteger.get());
}
}
CAS Other problems caused ?
- Only one atomic operation of shared variables can be guaranteed
- The spin CAS If it doesn't work for a long time , Will give CPU Bring the problem of high cost
边栏推荐
- 9.指针(上)
- MySQL中count(*)的实现方式
- 【手撕代码】单例模式及生产者/消费者模式
- 2.初识C语言(2)
- Mortal immortal cultivation pointer-2
- The latest tank battle 2022 - full development notes-3
- 透彻理解LRU算法——详解力扣146题及Redis中LRU缓存淘汰
- [the Nine Yang Manual] 2022 Fudan University Applied Statistics real problem + analysis
- [the Nine Yang Manual] 2017 Fudan University Applied Statistics real problem + analysis
- 3. Number guessing game
猜你喜欢
Leetcode.3 无重复字符的最长子串——超过100%的解法
3. C language uses algebraic cofactor to calculate determinant
Change vs theme and set background picture
仿牛客技术博客项目常见问题及解答(一)
3.C语言用代数余子式计算行列式
1.初识C语言(1)
View UI Plus 发布 1.3.0 版本,新增 Space、$ImagePreview 组件
4.分支语句和循环语句
C language to achieve mine sweeping game (full version)
Arduino+ds18b20 temperature sensor (buzzer alarm) +lcd1602 display (IIC drive)
随机推荐
1.C语言初阶练习题(1)
(super detailed II) detailed visualization of onenet data, how to plot with intercepted data flow
[hand tearing code] single case mode and producer / consumer mode
Arduino+ds18b20 temperature sensor (buzzer alarm) +lcd1602 display (IIC drive)
8. C language - bit operator and displacement operator
[graduation season · advanced technology Er] goodbye, my student days
5月27日杂谈
[modern Chinese history] Chapter V test
凡人修仙学指针-1
9.指针(上)
Write a program to simulate the traffic lights in real life.
5. Function recursion exercise
Implement queue with stack
关于双亲委派机制和类加载的过程
2022泰迪杯数据挖掘挑战赛C题思路及赛后总结
IPv6 experiment
3. C language uses algebraic cofactor to calculate determinant
仿牛客技术博客项目常见问题及解答(三)
Quickly generate illustrations
1.初识C语言(1)