当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
仿牛客技术博客项目常见问题及解答(一)
【手撕代码】单例模式及生产者/消费者模式
【九阳神功】2019复旦大学应用统计真题+解析
MySQL锁总结(全面简洁 + 图文详解)
5. Download and use of MSDN
[the Nine Yang Manual] 2020 Fudan University Applied Statistics real problem + analysis
用栈实现队列
5月14日杂谈
【毕业季·进击的技术er】再见了,我的学生时代
受检异常和非受检异常的区别和理解
3. C language uses algebraic cofactor to calculate determinant
Mode 1 two-way serial communication is adopted between machine a and machine B, and the specific requirements are as follows: (1) the K1 key of machine a can control the ledi of machine B to turn on a
Redis实现分布式锁原理详解
JS interview questions (I)
优先队列PriorityQueue (大根堆/小根堆/TopK问题)
2.初识C语言(2)
Floating point comparison, CMP, tabulation ideas
Mortal immortal cultivation pointer-1
The latest tank battle 2022 full development notes-1
Comparison between FileInputStream and bufferedinputstream