当前位置:网站首页>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
边栏推荐
- Redis实现分布式锁原理详解
- 仿牛客技术博客项目常见问题及解答(二)
- 【九阳神功】2018复旦大学应用统计真题+解析
- 4.分支语句和循环语句
- [modern Chinese history] Chapter 6 test
- (original) make an electronic clock with LCD1602 display to display the current time on the LCD. The display format is "hour: minute: Second: second". There are four function keys K1 ~ K4, and the fun
- A comprehensive summary of MySQL transactions and implementation principles, and no longer have to worry about interviews
- Share a website to improve your Aesthetics
- 【九阳神功】2019复旦大学应用统计真题+解析
- 重载和重写的区别
猜你喜欢
5. Download and use of MSDN
西安电子科技大学22学年上学期《基础实验》试题及答案
(original) make an electronic clock with LCD1602 display to display the current time on the LCD. The display format is "hour: minute: Second: second". There are four function keys K1 ~ K4, and the fun
MySQL锁总结(全面简洁 + 图文详解)
IPv6 experiment
A comprehensive summary of MySQL transactions and implementation principles, and no longer have to worry about interviews
编写程序,模拟现实生活中的交通信号灯。
MySQL Database Constraints
Wei Pai: the product is applauded, but why is the sales volume still frustrated
关于双亲委派机制和类加载的过程
随机推荐
9.指针(上)
Questions and answers of "signal and system" in the first semester of the 22nd academic year of Xi'an University of Electronic Science and technology
MySQL中count(*)的实现方式
Cloud native trend in 2022
hashCode()与equals()之间的关系
Rich Shenzhen people and renting Shenzhen people
【九阳神功】2017复旦大学应用统计真题+解析
强化学习系列(一):基本原理和概念
[面試時]——我如何講清楚TCP實現可靠傳輸的機制
Comparison between FileInputStream and bufferedinputstream
IPv6 experiment
【九阳神功】2019复旦大学应用统计真题+解析
用栈实现队列
[the Nine Yang Manual] 2021 Fudan University Applied Statistics real problem + analysis
(original) make an electronic clock with LCD1602 display to display the current time on the LCD. The display format is "hour: minute: Second: second". There are four function keys K1 ~ K4, and the fun
5月27日杂谈
Caching mechanism of leveldb
The latest tank battle 2022 - full development notes-3
MySQL Database Constraints
2022泰迪杯数据挖掘挑战赛C题思路及赛后总结