Java concurrent programming: CAS and java concurrent cas
Comparison and replacement of CAS (Compare and swap) is a technology used to design concurrent algorithms. Simply put, comparison and replacement use an expected value to compare with the current value of a variable. If the value of the current variable is equal to the expected value, replace the value of the current variable with a new value. This may sound a little complicated, but after you understand it, you will find it very simple. Next, let's take a deeper look at this technology.
CAS application scenarios
A common pattern in programs and algorithms is the "check and act" pattern. First check and then the operation mode occurs in the code first check the value of a variable, and then perform some operations based on this value. The following is a simple example:
03 |
private boolean locked = false ; |
05 |
public boolean lock() { |
The code above may cause many errors if it is used in a multi-threaded program, but forget it now.
As you can see, the lock () method first checks whether the locked> member variable is equal to false. If it is equal to, it sets locked to true.
If the same thread accesses the same MyLock instance, the above lock () cannot guarantee normal operation. If a thread checks the value of locked and sets it to false, a thread B is also checking the value of locked, or, before thread A sets the value of locked to false. Therefore, thread A and thread B may both see that the value of locked is false, and then both perform some operations based on this information.
To work well in a multi-threaded program, the "check then act" operation must be atomic. The atom means that the "check" Operation and "act" are executed as an atomic code block. There are no threads executing atomic blocks at the same time.
The following is a code example, which uses the synchronized keyword to reconstruct the previous lock () method into an atomic block.
03 |
private boolean locked = false ; |
05 |
public synchronized boolean lock() { |
Currently, the lock () method is synchronous. Therefore, at a certain time point, only one thread can execute it on the same MyLock instance.
The atomic lock method is actually an example of "compare and swap.
CAS for atomic operations
Now, the CPU has performed the atomic CAS operation. Since java 5, you can use some atomic classes in the java. util. concurrent. atomic package to use these functions in the CPU.
The following is an example of using the AtomicBoolean class to implement the lock () method:
1 |
public static class MyLock { |
2 |
private AtomicBoolean locked = new AtomicBoolean( false ); |
4 |
public boolean lock() { |
5 |
return locked.compareAndSet( false , true ); |
The locked variable is not a boolean type but a AtomicBoolean variable. There is a compareAndSet () method in this class. It compares the expected value with the value of the AtomicBoolean instance. If it is equal to the two, it replaces the original value with a new value. In this example, it compares the values of locked with false. If the value of locked is false, change it to true.
If the value is replaced, compareAndSet () returns true. Otherwise, false.
The advantage of using the CAS feature provided by Java 5 + instead of implementing it by yourself is that the built-in CAS feature in Java 5 + allows you to use the CAS feature of the CPU of the machine running in the underlying program. This will make the Code with CAS run faster.
Self-Concurrent Programming Network-ifeve.comLink:Java concurrent programming-CAS