当前位置:网站首页>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
边栏推荐
- MySQL中count(*)的实现方式
- FAQs and answers to the imitation Niuke technology blog project (III)
- IPv6 experiment
- 2. C language matrix multiplication
- 关于双亲委派机制和类加载的过程
- 7. Relationship between array, pointer and array
- MySQL lock summary (comprehensive and concise + graphic explanation)
- Leetcode. 3. Longest substring without repeated characters - more than 100% solution
- Floating point comparison, CMP, tabulation ideas
- Share a website to improve your Aesthetics
猜你喜欢
Leetcode. 3. Longest substring without repeated characters - more than 100% solution
View UI plus released version 1.3.0, adding space and $imagepreview components
[面试时]——我如何讲清楚TCP实现可靠传输的机制
C语言入门指南
fianl、finally、finalize三者的区别
学编程的八大电脑操作,总有一款你不会
使用Spacedesk实现局域网内任意设备作为电脑拓展屏
[面試時]——我如何講清楚TCP實現可靠傳輸的機制
Arduino+ds18b20 temperature sensor (buzzer alarm) +lcd1602 display (IIC drive)
西安电子科技大学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
4.分支语句和循环语句
MPLS experiment
[the Nine Yang Manual] 2021 Fudan University Applied Statistics real problem + analysis
[during the interview] - how can I explain the mechanism of TCP to achieve reliable transmission
仿牛客技术博客项目常见问题及解答(一)
六种集合的遍历方式总结(List Set Map Queue Deque Stack)
Change vs theme and set background picture
仿牛客技术博客项目常见问题及解答(二)
自定义RPC项目——常见问题及详解(注册中心)
View UI Plus 發布 1.3.1 版本,增强 TypeScript 使用體驗
Caching mechanism of leveldb
仿牛客技术博客项目常见问题及解答(三)
【手撕代码】单例模式及生产者/消费者模式
编写程序,模拟现实生活中的交通信号灯。
IPv6 experiment
[au cours de l'entrevue] - Comment expliquer le mécanisme de transmission fiable de TCP
Pit avoidance Guide: Thirteen characteristics of garbage NFT project
稻 城 亚 丁
Questions and answers of "Fundamentals of RF circuits" in the first semester of the 22nd academic year of Xi'an University of Electronic Science and technology