当前位置:网站首页>Atomic class and CAS
Atomic class and CAS
2022-07-19 00:08:00 【Tong++】
List of articles
Atomic classes
Why use atomic classes ?
Performance issues ,synchronized Keyword will let the thread that does not get the subelement enter BLOCKED state , Recover to... After contention for lock resources RUNNABLE state , In this process, the conversion between user mode and internal mode is required , The cost is high . although jdk 6 after ,synchronized With the transition from biased lock to lightweight lock to heavyweight lock , But after finally becoming a heavyweight lock , Performance is still low , In this case, we use java The atomic class in .
Atomic operation class :java.util.concurrent.atomic The series under the package is Atomic The first packaging class , Such as AtomicInteger、AtomicBoolean、AtomicLong.
The atomicity of atomic class is through volatile + CAS The realization of atomic operation .
CAS
CAS(Compare and swap): Compare and replace , It's an implementation of optimistic lock , Using the idea of spin , Is a lightweight locking mechanism .
Three values in memory
V: Memory value
A: Expected value , The value read again from memory during comparison
B: Update value , Updated value
If and only if expected A==V, Put the memory value V=B, Otherwise, do nothing .
Spin thought
The expected value is different from the value in memory every time , The difference is because the memory value has been modified by other threads , Therefore, we need to get the latest value again as the expected value , Judge again . And the thread constantly circulates to determine whether the memory value has been modified by other threads , This is the idea of spin . Through the bottom layer Unsafe Class compareAndSwapInt And so on .
shortcoming
1.cpu It's expensive ,CAS It's unlocked , Using the idea of spin , Threads don't block , If there are a large number of threads trying , It costs more .
2. There's no guarantee that the code block is atomic
3.ABA problem
ABA Problems and solutions
A thread transfers the memory value from A Change it to B, Again by B Change it to A, When another thread uses the expected value to judge , The expected value is the same as the memory value , Of the current thread CAS The operation cannot tell whether the memory value has been modified .
resolvent : Add version numbers by using classes . As before The memory value of is (A,1), Thread will (A,1) Revised to (B,2), Again by (B,2) It is amended as follows (A,3). At this point, another thread uses the expected value (A,1) And memory value (A,3) Compare , Just compare the version number 1 and 3, You can find that the data in the memory has been updated .
边栏推荐
- China carbon ceramic composite market research and investment forecast report (2022 Edition)
- How to deal with common errors in MySQL installation
- [Baidu PaddlePaddle] handwritten numeral recognition model deployment pad influence
- Vmware6.0 connection Qunhui iSCSI
- 乐观锁和悲观锁在kubernetes中的应用
- Redis has three modes -- master-slave replication, sentinel mode, and cluster
- 2022最新汉化camtasia studio电脑录制屏幕工具
- MySQL's redolog and binlog
- Mysql的redolog和binlog
- Iptables learning notes
猜你喜欢

负责任的AI | 三证加持,澳鹏中国又获ISO9001及27701认证

AtCoder Beginner Contest 259 D Circumferences

如何使用redis实现分布式缓存

VisionMaster与优傲机器人 UR5e 进行Modbus通讯

Mise en œuvre du modèle word2vec skip Gram

面试官:工作两年了,这么简单的算法题你都不会?

Four years after graduation, I changed three software testing jobs. Why am I still anxious?

Responsive form style transparent design

10. Find out the JVM operation status

数据接入平台(DIP)系列文章之一|功能及架构浅析
随机推荐
Bufferbloat and inflation
Pytest interface automated testing framework | introduction to pytest
应用的无状态设计
Ngrx store state management
9. Learn to check GC logs
学习要有激情
HMS core graphics and image technology shows the latest functions and application scenarios, and accelerates the construction of digital intelligence life
QT工具——moc、rcc、uic
Use of MySQL index
ES6之箭头函数
11. Find out the distribution of JVM objects
2022 - 07 - 16 notes d'étude pour le cours d'auto - culture du groupe 5 (tous les jours)
The relationship between filedescriptor and open file in liunx
WordPress主题分享:Avada主题v7.8.0免费下载 2022年最新版
【百度飞桨】手写数字识别模型部署Paddle Inference
OpenPose:使用部分親和場的實時多人 2D 姿勢估計
7. Common garbage collectors
In depth explanation of MySQL index
Go+mysql+redis+vue3 simple chat room, bullet 5: synchronize messages to MySQL using message queues and scheduled tasks
Pytest interface automation test framework | @pytest Fixture () decorator