Atomic classes use CAS. CAS stands for "Compare And Swap" and is a built on top of a hardware feature that allows operations such as "x = x + 1" to be atomic.
CAS stands for "Compare And Swap" and is a built on top of a hardware feature that allows operations such as "x = x + 1" to be atomic.
Note that one of the principles of CAS is a loop where the CAS component continuously tries to complete the CAS operation successfully. i.e. it keeps trying until it suceeds. Apart from CAS counters there are also concurrent data structures which are built on top of CAS functionality.
One thing to note about CAS functionality is that it works well when there is not a lot of contention between threads. i.e. it works well when there are not a massive amount of threads all competing to perform the CAS operation.
Data Structures like ConcurrentHashMap also use CAS. So ConcurrentHashMap is good for medium contention but will work less well under extreme contention.
Where there is extreme contention consider replacing the component which is using CAS with alternative mechanisms:
Locks
synchronized keyword.
use the Collections.synchronize(..) methods to wrap a single threaded map to make it good for multithreaded environments.
There are several different CAS classes:
Page Author: JD