What is the version number of mechanisms, CAS algorithm?

1. The version number of mechanisms

Typically plus a database in the data table version number versionfield, the number of times data is modified when the expression data is modified, version value plus 1. When a thread A to update the data values will be read version value while reading data, when submitting the update, if just to read the version when the update is version equal value in the current database, or retry update until the update is successful.

2. CAS algorithm

Compared with the exchange (compare and swap), is a lock-free algorithms that achieve variable synchronization between multiple threads without a lock, which is synchronized variable in the absence of the thread is blocked, it is also called synchronous non-blocking (Non-blocking synchronization). CAS algorithm involves three operands: the need to read and write memory value V, a value of the comparison A, the proposed new written value B. If and only if the value is equal to V A, CAS is updated with the new value V B values ​​atomically, it will not perform any operations (and swap operation is an atom). Under normal circumstances a self-rotational operation, i.e. continuously retries.

CAS algorithm drawbacks:

1. ABA problem

If a variable V when the initial read is the value of A, and checks to the assignment at the time of preparation it is still value A, then we can explain its value has not been revised yet another thread? Obviously not, because during that time its value may be changed to a different value, then was changed back to A, that CAS operation would think that it was never to change, a problem known as "ABA" CAS operations problem.

2. Long cycle cost big time

Since the cycle CAS (that is, the cycle has been unsuccessful execution until it succeeds) if not successful for a long time, will bring a very large execution overhead CPU. If the JVM can support the processing mentioned pauseinstruction, then there will be some efficiency improvement. pause command has two effects, which can delay the first instruction execution pipeline (Pipeline), so that the CPU does not consume too many resources to perform, the delay time is implementation dependent, in some processor time delay is zero. The second time it can be avoided in order to launch the cycle of conflict because of memory (memory order violation) caused by CPU pipeline is cleared (CPU pipeline flush), in order to improve the efficiency of the CPU.

3. atomic operation can only guarantee a shared variable

CAS valid only for a single shared variable, when the operation involves a plurality of shared variables across CAS invalid. But JDK1.5 start, provided AtomicReferenceto ensure atomicity references between objects, you can put multiple variables on an object in the conduct CAS operation. Therefore, we can use the lock or with the plurality of shared variables AtomicReference combined into a shared variable to operate. Adaptive spin solution is to "lock contention time of uncertainty" problem, the goal is to reduce the cost of thread switching.

Guess you like

Origin juejin.im/post/5d16d1d1f265da1b827ab05f