Understanding of CAS

What is CAS?

Compare and set

public class CASDemo{
	public static void main(String[] args){
		AtomicInteger atomicInteger=new AtomicInteger(5);
		
		boolean flag1=atomicInteger.compareAndSet(5,2019);
		//result==2019
		int result1=atomicInteger.get();
		
		boolean flag2=atomicInteger.compareAndSet(5,2001);
		//修改失败,result==2019
		int result1=atomicInteger.get();
	}
}

atomicInteger.compareAndSet(a,b)

1) The previous value is the expected value, and the value of the expected physical memory is the same as this value

2) If the actual value is the same as the expected value, change the value to b and write it into the physical memory. If it is different, don’t write it

 

CAS underlying principle

Unsafe class getAndAddInte() method + CAS thought

var1---AtomicIntger object itself

var2---The reference address of the object value

var4---The amount that needs to be changed

var5---The real value in the main memory found by using var1 and var2

Use the current value of the object to compare with var5:

If the same, update var5+var4 and return true

If they are different, continue to take the value and compare again until the update is complete

 

Here are the reasons for using CAS instead of synchronized locks:

If locks are used, consistency is ensured, but concurrency is reduced. The do-while model can be used to achieve spin and ensure concurrency

 

Assume that two threads, thread A and thread B, perform the getAndAddInt operation at the same time:

1) The original value of value in AtomicInteger is 3, that is, the value of AtomicInteger in main memory is 3. According to the JMM model, thread A and thread B each hold a copy of the value of 3 to their respective working memory

2) Thread A gets the value 3 through getIntVolatile (var1, var2), then thread A is suspended

3) Thread B also obtains the value 3 through the getIntVolatile method. At this time, thread B is not suspended and executes the compareAndSwapInt method. The comparison memory value is also 3, and the memory value is successfully modified to 4

4) At this time, thread A resumes, executes the compareAndSwapInt method, and finds that the value number 3 in his hand is inconsistent with the value number 4 of the main memory, indicating that the value has been modified by other threads first, then the modification of thread A fails this time , Can only read it again

5) Thread A re-acquires the value value, because the variable value is modified by volatile, so other threads modify it, thread A can always see, thread A continues to execute compareAndSwapInt for comparison and replacement until it succeeds

 

to sum up

CASE:

Compare the value in the current working memory with the value in the main memory, if they are the same, perform the specified operation, otherwise continue to compare until the values ​​in the main memory and the working memory are consistent

 

CAS application:

CAS has 3 operands, the memory value V, the old expected value A, and the updated value B to be modified. If and only if the expected value A and the memory value V are the same, modify the memory V to B, otherwise the instructions will not be done. . Repeat the above process until it succeeds

 

Disadvantages of CAS:

1) Long cycle time and high overhead (do-while)

2) Only guarantee the atomic operation of a shared variable

3) Bring out the ABA problem (refer to the blog post https://blog.csdn.net/di_ko/article/details/115066900 )

 

 

Guess you like

Origin blog.csdn.net/di_ko/article/details/115065990