What is a CAS Operation in Java ?

CAS is also known a Compare-And-Swap operation. In a CAS operation, the processor provides a separate instruction that can update the value of a register only if the provided value is equal to the current value.

CAS operation can be used as an alternate to synchronization.

Let say thread T1 can update a value by passing its current value and the new value to be updated to the CAS operation. In case another thread T2 has updated the current value of previous thread, the previous thread T1’s current value is not equal to the current value of T2. Hence the update operation fails.

In this case, thread T1 will read the current value again and try to update it.



You May Interest

What is a WeakHashMap in Java ?

What are the Differences Between Collection and Collections in Ja ...

How Can We Take Thread Dump in Java ?

What is a Shutdown Hook in Java ?

What are the Differences Between a List and Set Collection in Jav ...