4

A small friend sent me a private message, he said he encountered a problem about the CAS mechanism, he thought the interviewer asked about CAS to achieve single sign-on.

I thought to myself, I am familiar with this question, and then I followed the idea of single sign-on to answer, but the interviewer kept shaking his head.

He came to tell me that he didn't make it clear that his answer was so good at the end of the interview, why didn't he send me an offer on the spot?

Actually, the interviewer asked about the CAS mechanism in concurrent programming.

Let's take a look at the answers of ordinary people and experts to the CAS mechanism.

Ordinary people:

CAS is an operation used to achieve atomicity in concurrent programming. Well, it is similar to an optimistic locking mechanism, which can ensure the atomicity of changes to the value of shared variables under concurrent conditions.

Well, in classes like AtomicInteger, the CAS mechanism is used. Ok…

Expert:

CAS is a method in the Unsafe class in Java. Its full name is CompareAndSwap, which means compare and exchange. Its main function is to ensure the atomicity of the modification of shared variables in a multi-threaded environment.

Let me give an example, for example, there is such a scene, there is a member variable state, the default value is 0,
carbon20220318001

A method is defined doSomething() . The logic of this method is to judge whether the state is 0, and if it is 0, modify it to 1.

There seems to be no problem with this logic, but in a multi-threaded environment, there will be atomic problems, because here is a typical Read-Write operation.

Under normal circumstances, we will add a synchronization lock to the doSomething() method to solve the atomic problem.

However, adding synchronization locks will bring about performance losses, so for such scenarios, we can use the CAS mechanism to optimize

This is the optimized code
carbon20220318002

In the doSomething() method, we call the compareAndSwapInt() method in the unsafe class to achieve the same purpose, this method has four parameters,

They are: the current object instance, the offset of the member variable state in the memory address, the expected value 0, and the expected value 1 after the change.

The CAS mechanism will compare whether the value corresponding to the state memory address offset is equal to the incoming expected value of 0. If they are equal, directly modify the state value in the memory address to 1.

Otherwise, return false, indicating that the modification failed, and this process is atomic, and there will be no thread safety problems.

CompareAndSwap is a native method. In fact, it will eventually face the same problem, that is, first read the state value from the memory address, then compare it, and finally modify it.

No matter what level this process is implemented at, there will be atomicity problems.

Therefore, in the underlying implementation of CompareAndSwap, in a multi-core CPU environment, a Lock instruction will be added to lock the cache or bus, thereby ensuring the atomicity of comparing and replacing these two instructions.

CAS is mainly used in concurrent scenarios, and there are two typical usage scenarios.

  1. The first is the atomic implementation of Atomic in JUC, such as AtomicInteger and AtomicLong.
  2. The second is to realize the mutual exclusion nature of multi-thread competition for shared resources, such as AQS, ConcurrentHashMap, ConcurrentLinkedQueue, etc.

The above is my understanding of the problem.

Summarize

Recently, everyone has also discovered the changes in the content of my article in the expert answer section.

Some friends said, how can you still bring pictures in the interview, obviously cheating.

In fact, the main reason is that many of the recent interview questions are low-level, and the bottom-level content covers a wide range of knowledge, and everyone has almost no contact with them.

Therefore, if I want to pass on this knowledge to everyone, I have to do a lot of graphic and content structure design, otherwise everyone will be confused after reading it.

Ok, this issue of the interview series of ordinary people vs masters is over here. Friends who like it remember to like and favorite.

I'm Mic, a Java programmer who has worked for 14 years, see you next time.
file

Copyright notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless otherwise stated. Please indicate the source for Mic带你学架构 !
If this article is helpful to you, please help to follow and like, your persistence is the driving force for my continuous creation. Welcome to follow the WeChat public account of the same name to get more technical dry goods!

跟着Mic学架构
810 声望1.1k 粉丝

《Spring Cloud Alibaba 微服务原理与实战》、《Java并发编程深度理解及实战》作者。 咕泡教育联合创始人,12年开发架构经验,对分布式微服务、高并发领域有非常丰富的实战经验。