Article Directory
1, CPU instruction to CAS support (CPU CAS Directive is atomic)
Perhaps we may have this question, assume that there are multiple threads to perform CAS operations and CAS have many steps, it is possible to determine the same as V and E after the assignment, the switch thread, changed the value. Cause the data to be inconsistent? The answer is no, because CAS is a system primitive, the primitive language belongs to the operating system language category, is composed of a number of instructions, used to complete a function of a process, and the implementation of the primitive must be continuous, not allowed to be interrupted during execution, that is, the CAS is a CPU atomic instructions, does not cause the so-called data inconsistency problem.
2, and the atomic operation in the contract Class (atomic series)
Through the previous analysis we have a basic understanding of the principle of unlocked CAs and the Java pointer class unsafe class has a more comprehensive understanding, the following further analysis of the application of CAs in Java, namely, and the contract of the Atomic Operation Class (Atomic series), from the beginning of JDK 1.5 to provide java.util.concurrent.atomic Package, which provides a number of CAS-based atomic operation classes, easy to use, high performance, mainly divided into the following 4 types.
It can be found that the interior of the Atomicinteger Atom class is almost based on the method of CAS related operation in the earlier analysis of the unsafe class, which also proves that Atomicinteger is based on the lock-free implementation, which focuses on the self-increment operation realization Process, Other methods have the same principle of self-amplification.
3. The ABA problem of CAs and its solution
Suppose such a scenario, when the first thread performs a CAS (v,e,u) operation, before getting to the current variable V, ready to modify to the new value U, the other two threads have continuously modified the value of the variable V for two times, so that the value is restored to the old value, so that we can not correctly determine whether the variable has been modified, such as
This is the typical CAs ABA problem, the general situation of this situation found that the probability is relatively small, may have occurred and will not cause any problems, for example, we do add and subtract, do not care about the number of the process, then there is no relationship between ABA problems. But in some cases it still needs to be prevented, so how to solve it? To solve the ABA problem in Java, we can use the following two atomic classes
Atomicstampedreference class
-
The Atomicstampedreference Atomic class is a timestamp object reference that, after each modification, atomicstampedreference not only sets a new value but also records the time of the change. When Atomicstampedreference sets the object value, both the object value and the timestamp must satisfy the expected value for the write to succeed, which resolves the dilemma of whether the values have been modified while repeatedly reading and writing
Reference article:
1. CAs in Java
Java CAs Summary