当前位置:网站首页>Multi thread learning notes -1.cas
Multi thread learning notes -1.cas
2022-07-26 19:02:00 【I was born to be talented~~】
List of articles
Muke multi thread learning tutorial
1. What is? CAS
1.1 brief introduction
- Essentially, CPU Special instructions for
- Because it's an instruction , from CPU It guarantees atomicity , So it's thread safe


- Because it's an instruction , from CPU It guarantees atomicity , So it's thread safe
1.2. CAS The equivalent code of ( semantics )
/** * describe : simulation CAS operation , Equivalent code */
public class SimulatedCAS {
private volatile int value;
// The whole method corresponds to an instruction ==CAS Instructions
public synchronized int compareAndSwap(int expectedValue, int newValue) {
int oldValue = value;
if (oldValue == expectedValue) {
value = newValue;
}
return oldValue;
}
}
1.3 Case presentation - Simulate thread contention
- Create two threads , Perform simulation at the same time CAS operation
public class TwoThreadsCompetition implements Runnable {
private volatile int value;
public synchronized int compareAndSwap(int expectedValue, int newValue) {
int oldValue = value;
if (oldValue == expectedValue) {
value = newValue;
}
return oldValue;
}
// Simulate two threads competing
public static void main(String[] args) throws InterruptedException {
TwoThreadsCompetition r = new TwoThreadsCompetition();
r.value = 0;// The default is expectation 0
Thread t1 = new Thread(r,"Thread 1");
Thread t2 = new Thread(r,"Thread 2");
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println(r.value);
}
@Override
public void run() {
compareAndSwap(0, 1);
}
}
- Thread debugging - Enable multithreading debugging

- Execute first 1 Threads - take value Make changes

- Re execution 2 Threads , It is found that the expected value is different from the existing value , Update failed

3. Application scenarios
3.1 Optimism lock
3.2 Concurrent container
- for example
ConcurrentHashMap
3.3 Atomic classes

With AtomicInteger For example , analysis CAS Realization principle


volatile modification
- Ensure the visibility of variables
Unsafe class

Unsafe Class compareAndSwapInt

summary

5. Shortcomings and summary
5.1 ABA problem
- If only the change of value is considered , There may be A Changed by another thread to B, Finally, it was changed to A, such , The current thread will mistakenly think that there is no version change , In this way, some operations in the subsequent sequence will have hidden dangers
- The solution strategy is : Copy database , Add a version number to each version for maintenance , Each comparison does not just look at the value , Also compare whether the version number has changed
5.2 Spin time is too long
- If the competition is fierce , The lock can't be reached , Will spin all the time , Will consume CPU
边栏推荐
- MySQL日志介绍
- 议程速递 | 7月27日分论坛议程一览
- 还在用Xshell?推荐这个更现代的终端连接工具
- 455. Distribute cookies [double pointer ++i, ++j]
- FTP协议
- JS map usage
- Operation: skillfully use MySQL master-slave replication delay to save erroneously deleted data
- rancher部署kubernetes集群
- Still using xshell? Recommend this more modern terminal connection tool
- NFT digital collection system development: fellow uncle first promoted the blessing series digital collection, which will be sold out immediately
猜你喜欢
随机推荐
JS刷题计划——链表
Write a thesis and read this one
.Net CLR GC 动态加载短暂堆阈值的计算及阈值超量的计算
Seata 入门简介
如何成为一名优秀的测试/开发程序员?专注谋定而后动......
工赋开发者社区 | 定了!就在7月30日!
[yuntu said] issue 246 digital asset chain - your God of digital asset property protection!
MySQL数据库命令大全
The principle of database index, why use b+ tree, why not binary tree?
深入理解为什么不要使用System.out.println()
Operation: skillfully use MySQL master-slave replication delay to save erroneously deleted data
MySQL exercises elementary 45 questions (Unified table)
【考研词汇训练营】Day 13 —— reliance,expert,subject,unconscious,photograph,exaggeration,counteract
14. Gradient detection, random initialization, neural network Summary
NFT digital collection development: digital collections help enterprise development
How to design test cases well
Exploratory software testing
网络协议:TCP/IP协议
2022g1 industrial boiler stoker certificate question bank and simulation examination
此章节用于补充3









