CAS explanation of Java Concurrent Programming
CAS (compare and swap) compare and replace is a technique used in designing concurrent algorithms. In short, compare and replace is to compare an expected value with the current value of a variable. If the value of the current variable is equal to our expected value, use a new value to replace the value of the current variable. This may sound a little complicated, but in fact, you will send it after you understand it Now it's very simple. Next, let's have an in-depth understanding of this technology.
Usage scenarios of CAS
A common pattern in programs and algorithms is the "check and act" pattern. Check before operation mode occurs in the code. First check the value of a variable, and then do some operations based on this value. Here is a simple example:
The above code will make a lot of errors if used in multithreaded programs, but please forget it now.
As you can see, the lock () method first checks whether the locked > member variable is equal to false. If so, set locked to true.
If the same thread accesses the same mylock instance, the lock () above will not work properly. If a thread checks the locked value and sets it to false, at the same time, a thread B also checks the locked value, or before thread a sets the locked value to false. Therefore, thread a and thread B may both see the value of locked as false, and then both do some operations based on this information.
In order to work well in a multithreaded program, the "check the NACT" operation must be atomic. Atomic means that the "check" operation and "act" are executed as an atomic code block. There are no multiple threads executing atomic blocks at the same time.
The following is a code example, which reconstructs the previous lock () method into an atomic block with the synchronized keyword.
Now the lock () method is synchronized, so only one thread can execute it on the same mylock instance at a time.
The atomic lock method is actually an example of "compare and swap".
CAS as atomic operation
Now the atomic CAS operation has been executed inside the CPU. Since Java 5, you can use Java util. concurrent. Some atomic classes in the atomic package use these functions in the CPU.
The following is an example of using atomicboolean class to implement lock() method:
The locked variable is no longer of type Boolean, but atomicboolean. There is a compareandset () method in this class. It uses an expected value to compare with the value of the atomicboolean instance. If the two are equal, it uses a new value to replace the original value. In this example, it compares the value of locked with false. If the value of locked is false, it will be changed to true.
Compareandset() returns true if the value is replaced; otherwise, it returns false.
The advantage of using the CAS feature provided by Java 5 + instead of using its own implementation is that the built-in CAS feature in Java 5 + allows you to make use of the CAS feature of the CPU of the underlying machine running your program. This will make the code with CAS run faster.
summary
The above is all about CAS detailed explanation of Java Concurrent Programming. I hope it will be helpful to you. Interested friends can continue to refer to this site: detailed explanation of high concurrency solutions of Java system, re-entry lock and read-write lock of Java Concurrent Programming, detailed explanation of semaphore counting semaphore of Java Concurrent Programming, etc. if you have any questions, you can leave a message at any time. Your valuable opinions are welcome, and the editor will reply to you in time. Thank you for your support!