Java concurrent programming: CAS and java concurrent cas

Source: Internet
Author: User

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:

01 class MyLock {
02  
03     private boolean locked = false;
04  
05     public boolean lock() {
06         if(!locked) {
07             locked = true;
08             return true;
09         }
10         return false;
11     }
12 }

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.

01 class MyLock {
02  
03     private boolean locked = false;
04  
05     public synchronized boolean lock() {
06         if(!locked) {
07             locked = true;
08             return true;
09         }
10         return false;
11     }
12 }

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);
3  
4     public boolean lock() {
5         return locked.compareAndSet(false, true);
6     }
7  
8 }

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.