当前位置:网站首页>[interview ordinary people vs Expert Series] can you talk about CAS mechanism?
[interview ordinary people vs Expert Series] can you talk about CAS mechanism?
2022-04-21 16:22:00 【Learn architecture from mic】
A little friend confided in me , He said he met a man about CAS The problem of mechanism , He thought the interviewer was asking CAS Single sign on .
assume , I'm familiar with this question , Then follow the idea of single sign on to answer , As a result, the interviewer kept shaking his head .
He came to talk to me , At the end of the interview, I didn't want to answer so well , Why didn't you send it to me on the spot offer Well ?
actually , The interviewer asked about... In concurrent programming CAS Mechanism .
Now let's take a look at ordinary people and experts for CAS The answer of the mechanism
Ordinary people :
CAS, It is an operation used to realize atomic functions in concurrent programming , Um. , It is similar to an optimistic lock mechanism , It can ensure the atomicity of changes to the value of shared variables in the case of concurrency .
Um. , image AtomicInteger In this class , It uses CAS Mechanism . Um. …
master :
CAS yes Java in Unsafe Methods in class , Its full name is CompareAndSwap, Compare and exchange . Its main function is to ensure that in a multithreaded environment , Atomicity of modifications to shared variables .
Let me give you an example , For example, there is such a scene , There is a member variable state, The default value is 0,
Defines a method doSomething(), The logic of this method is , Judge state Is it 0 , If 0, Just change it to 1.
There seems to be no problem with this logic , But in a multithreaded environment , There will be atomic problems , Because this is a typical ,Read - Write The operation of .
In general , We will be in doSomething() This method adds a synchronization lock to solve the atomicity problem .
however , Add synchronization lock , It will bring performance loss , therefore , For such scenarios , We can use CAS Mechanism to optimize
This is the optimized code 
stay doSomething() In the method , We call unsafe Class compareAndSwapInt() Methods to achieve the same purpose , This method has four parameters ,
Namely : Current object instance 、 Member variables state Offset in memory address 、 Expected value 0、 Expected value after change 1.
CAS The mechanism will be more state The value corresponding to the memory address offset and the expected value passed in 0 Whether it is equal or not , If equal , Just modify the memory address directly state The value of is 1.
otherwise , return false, Indicates that the modification failed , And this process is atomic , There will be no thread safety issues .
CompareAndSwap It's a native Method , In fact, it will eventually face the same problem , Is to read from the memory address first state Value , And then compare , Finally, I will revise .
No matter at what level this process is realized , There will be atomic problems .
So ,CompareAndSwap In the underlying implementation of , In multicore CPU In the environment , One will be added Lock Instruction locks the cache or bus , So as to ensure the atomicity of comparing and replacing the two instructions .
CAS Mainly used in concurrent scenarios , There are two typical usage scenarios .
- The first is J.U.C Inside Atomic Atomic realization of , such as AtomicInteger,AtomicLong.
- The second is to realize the mutually exclusive nature of multi-threaded competition for shared resources , For example AQS、ConcurrentHashMap、ConcurrentLinkedQueue And so on .
The above is my understanding of this problem .
summary
Recently, you also found the change of the content of my article in the expert answer part .
Some of the lads said , How can you bring pictures to the interview , Obviously cheating .
In fact, the main reason is that many recent interview questions are at the bottom , The content of the bottom layer covers a wide range of knowledge , We hardly have any contact with .
therefore , If I want to pass this knowledge on to you , You have to do a lot of graphic and content structure design , Otherwise, everyone will still look confused after reading .
well , Ordinary people in this issue VS That's the end of the expert interview series , Remember to like your friends .
I am a Mic, A job 14 Year of Java The programmer , Let's see you next time .
Need interview materials or interview questions
↓↓↓↓↓↓↓↓
版权声明
本文为[Learn architecture from mic]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211619504587.html
边栏推荐
猜你喜欢

.NET Core Swagger配置

elmentUI下拉框实现全部功能

云呐:资产密集型企业固定资产管理系统的基本功能特点

云呐:大型医疗设备资产管理系统贵吗?医院资产管理的主要内容

php如何把负数转为正整数

必刷|2022年重庆最新八大员之(安全员)模拟题库及答案

The console displays VM + number + file name to debug

Haotian Xuhui signed a contract with Changyang technology to jointly build a new ecosystem of industrial Internet security

关于nodejs fs readFileSync 过程中强行关机 拉闸 断电 导致文件损坏的问题解决方案

2022数二真题
随机推荐
Arthas Tunnel使用
云呐:大型医疗设备资产管理系统贵吗?医院资产管理的主要内容
OJ每日一练——最大公约数与最小公倍数
HMC foundation big Ma robot synchronization token HMC (seaman) will launch pancakeswap
mysql查询某一个字段是否包含中文汉字
菜粕期货是那个交易所上市的?新手怎么期货开户最安全?
【2023校招刷題】華為性格測評(綜合測評)戰略指南
Transformer model technology long article
Must brush the 2022 Chongqing latest fire facility operator simulation question bank and answers
嵌入式GUI盘点-你了解几款?
What is the anti correlation principle? How to choose the anti Association fingerprint browser? What are the criteria?
云呐:医院固定资产管理存在的问题及原因,资产管理系统的实施
手把手教你基于LXD用OAK-D和ROS noetic做ORB SLAM3
排序课后练习题
Online dictionary website
CRM系统可以帮助改善客户体验吗?
Campus talking notes (5)
全国查询水电气费免费接口(一)
Root unlock problem
Case of kingbasees v8r3 cluster deleting data nodes online in Jincang database