Cas,compare and swap is a technique that is often used to design concurrency algorithms by comparing and replacing them. CAS has three operands: Memory value V, old expected value A, value B to be modified, if and only if the expected value A and the memory value of V are the same, the memory value is modified to B and returns true, otherwise nothing is done and returns false.
The general principle of CAS algorithm is: Before the calculation of variables (such as the + + operation), first read the original variable value, called旧的预期值 A, and then gets the current in-memory value before the update, called当前内存值 V, ifA==VIf the variable is never modified by another thread, the new value B will be written, ifA!=Vthe variable has been modified by another thread, and the current thread should do nothing;
Problem:
---the problem of ABA
Since the CAS design mechanism is to get the value of a variable for a couple of moments (initial expected value and current memory value) and compare updates, it is not perceptible for CAs if the value of the variable is changed from a to B and then to a in the time interval between getting the initial expected value and the current memory value. But in fact the variable has changed, the workaround is to add the version number each time, and each update to version number +1, so that when the ABA problem occurs through the version number can be learned that the variable has been changed
This capability is provided by the Atomicstampedreference class after JDK 1.5, where the Compareandset method is to first check whether the current reference is equal to the expected reference, and if the current flag is equal to the expected flag, and if all is equal, It atomically sets the reference and the value of the flag to the given new value.
-----Long cycle time overhead
(the so-called long cycle time overhead problem is that when the CAS decision variable is modified, the modification is discarded, But often in order to ensure the correctness of the data, the calculation will be re-launched in a circular way CAs, if multiple CAs failed to determine, it will generate a lot of time consumption and performance waste ; If the JVM can support the pause command provided by the processor, the efficiency will be improved. The pause command has two functions, first it can delay the pipelined execution instruction (de-pipeline), so that the CPU does not consume excessive execution resources, the delay time depends on the specific implementation of the version, on some processors the delay time is zero. Second, it avoids the CPU pipelining being emptied (CPU pipeline flush) when exiting the loop due to memory order collisions (violation), which improves CPU execution efficiency. )
-----can only guarantee atomic operation of a shared variable
(CAs are only valid for a single shared variable, and CAS is not valid when the operation involves multiple shared variables; from JDK 1.5, atomicreference classes are provided to guarantee atomicity between reference objects, and you can put multiple variables in an object for CAS operations)
-
unsafe is the core class of CAs, and Java cannot directly access the underlying operating system, but is accessed through local (native) methods. However, the JVM has opened a backdoor, with a class unsafe in the JDK, which provides hardware-level atomic operation .
-
valueoffset represents the offset address of the variable value in memory, Because unsafe is to get the original value of the data based on the memory offset address.
-
Atomicinteger: assume that thread A and thread B now perform the getandadd operation simultaneously:
-
The value in Atomicinteger contains the original value of 3, that is, the value of Atomicinteger in main memory is 3, according to the Java memory model, thread A and thread B each hold a copy of value of 3.
-
Thread A is obtained by Getintvolatile (var1, Var2) method to value 3, thread switch, thread a hangs.
-
Thread B obtains a value of 3 through the Getintvolatile (var1, var2) method and compares the memory value with the Compareandswapint method by 3, succeeds, modifies the memory value to 2, thread switches, and thread B hangs.
-
Thread A recovery, using the Compareandswapint method, compares the value of 3 in the sender with the memory value 4, when value is being modified by another thread, and thread A cannot modify the value .
-
The thread's Compareandswapint implementation, looping judgment, re-fetching value, because value is a volatile variable, so the thread modifies it, and thread A is always able to see it. Thread A continues to compare and replace with Compareandswapint until the Compareandswapint modification returns true successfully.
Throughout the process, the use of CAS guarantees thread safety for value modification.
This article is from the "10093778" blog, please be sure to keep this source http://10103778.blog.51cto.com/10093778/1931487
Java CAs Understanding