One of the nervous mistakes made by the 2023 fresh graduates in the Java interview: CAS mistakenly said it was a happy lock - making the interviewer laugh

Sourced from: XX network, sorry if it offended

Interviewer: What is CAS?

Me: This is simple, happy lock

Interviewer: WTF?

Me: Looking confident, yes, that’s it

Interviewer: Laughing loudly, haha~, go back and wait for the notification

Me: WFT? What are you laughing at?

Audience: I was rejected just this afternoon, and I was instantly healed by you. . Depression has been cured

CAS meaning:

compare and swap, translated as compare and replace. Memory address V, old expected value A, new value to be modified B. Commonly known as: optimistic lock

So what is the definition of optimistic locking? :

Optimistic locking maintains an optimistic attitude towards data conflicts. When operating data, the operated data will not be locked (this allows multiple tasks to operate on the data in parallel), and only through a mechanism when the data is submitted. To verify whether there is a conflict in the data (the general implementation method is to add the version number and then compare the version number);

It is essentially an instruction set of the CPU that can provide an operation. This operation is a non-stop for loop. This instruction is used to obtain the address of a certain memory. If it is obtained, it means that the lock is obtained.

Let’s take an example of the method in AtomicInteger in the underlying source code:

It is a do while loop operation. There are 2 operations here.

1. v = getIntVolatile(o, offset):

Used to get the integer value corresponding to the offset address in the object. Among them, o represents the object, and offset represents the offset. This method will return the value in the shared memory, and control the visibility of the value through volatile to ensure that the value obtained from the memory is the latest value.

2. weakCompareAndSetInt(o, offset, v, v + delta)

Used to compare values ​​in memory to see if the old values ​​are equal. If they are equal, write the modified value to memory and return true. Indicates the modification was successful.

Among them, o represents the object, offset represents the offset, v and v+delta represent the expected value and new value respectively (the same means that no other threads will modify this value during this period). This method is atomic, which means that it will not be interrupted by other threads during execution.

Its bottom layer is a Native method:

English description:

If the Java variable currently holds the expected value, it is automatically updated to x.
This operation has memory semantics of volatile reads and writes. Corresponds to C11 atomic_compare_exchange_strong (C++).
Return:
true if successful

Wrote a demo:

1. Simply add one and compare the expected value with the actual value

2. Let two threads compete for the job

What problems will arise with CAS:

1. Yes, it’s the ABA problem you’re thinking about.

Solution: Use the version number, and compare the timestamp/version number every time when comparing and exchanging.

2. Underlying implementation: If the while operation cannot be performed, it will continue to loop. If it loops for a long time, it will cause the CPU to idle and consume resources.

Solution: Need to control the number of spins

3. It can only guarantee the atomic operation of a variable, but not the atomicity of a code block.

Guess you like

Origin blog.csdn.net/weixin_42450130/article/details/132643331