1. Non-blocking algorithm
Non-blocking algorithms belong to concurrent algorithms that can safely derive their threads, not by locking, but through low-level atomic hardware native forms-such as comparisons and exchanges. The design and implementation of non-blocking algorithms is extremely difficult, but they provide better throughput and provide better defenses against survival problems such as deadlocks and priority inversion. Use the underlying atomized machine instructions to replace the lock, such as comparison and Exchange (CAS,COMPARE-AND-SWAP).
2. Pessimistic technology
Exclusive locks are a pessimistic technique. It assumes that the worst-case scenario occurs (if not locked, other threads break the object state), and the lock protects the object state even if the worst-case scenario does not occur.
3. Optimistic technology
reliance on conflict monitoring. Update first, if monitoring conflicts occur, discard the update and try again, otherwise the update succeeds. Now the processors have atomized read-and-write instructions, such as comparison and Exchange (CAS,COMPARE-AND-SWAP).
4.CAS operation
CAS has 3 operands, memory value V, old expected value A, new value B to modify. If and only if the expected value A and memory value v are the same, the memory value V is modified to B, otherwise nothing is done. The CAs typical usage pattern is to read a from V first, to compute the new value B based on a, and then to convert the value in V from A to B (as long as there is no thread in this period to modify the value of V to another value) by using the CAs atom.
Listing 3. Code that describes the behavior (not performance) of the comparison and exchange
Copy Code code as follows:
public class Simulatedcas {
private int value;
public synchronized int GetValue () {return value;}
public synchronized int Compareandswap (int expectedvalue, int newvalue) {
int oldValue = value;
if (value = = Expectedvalue)
value = newvalue;
return oldValue;
}
}
Listing 4. Use comparison and exchange to implement counters
Copy Code code as follows:
public class Cascounter {
private Simulatedcas value;
public int GetValue () {
return Value.getvalue ();
}
public int increment () {
int oldValue = Value.getvalue ();
while (Value.compareandswap (OldValue, OldValue + 1)!= OldValue)
OldValue = Value.getvalue ();
return oldValue + 1;
}
}
5. Atomic Variables
Atomic Variables support atomic update operations without lock protection, and the bottom layer is implemented with CAS. There are 12 atomic variables, which can be divided into 4 groups: Scalar class, update class, array class, and compound variable class. The most commonly used atomic variables are scalar classes: Atomicinteger, Atomiclong, Atomicboolean, and Atomicreference. All types support CAs.
6. Performance comparison: Lock and Atomic variable
In the medium to low degree of competition, the atom variable can provide very high scalability, the performance of atomic variables exceeds the lock, while in high-intensity competition, lock can more effectively avoid competition, lock performance will exceed the performance of atomic variables. But in more realistic situations, the performance of the atomic variable will exceed the performance of the lock.