Java:Concurrency
What is CAS?
CAS stands for “Compare and Swap”. This is a technique used when designing concurrent algorithms. The approach is to compare the actual value of the variable to the expected value of the variable and if the actual value matches the expected value, then swap the actual value of the variable for the new value passed in. It is like I know that this variable should be 3 and I want to change it to 4. Since this is a multithreaded environment, I know that others might be working on the same variable. So I first check if the value of the variable is 3 as I thought and if yes, then I change it to 4. If I see that the variable is 5 now, then that means someone else is working on it and so let me not touch it at this time.
What is “Check then Act”?
“Check then Act” is a general approach that applies in the context of concurrency problems. CAS is an example of following this approach.
An example of logic of applying this principle is as below.
if(!locked) {
locked = true;
return true;
}
return false;
Meaning if it is already locked, you don’t need to lock again. So you first check and only if required, act.
How is this not thread safe?
Two threads might do the check at the same time and think that it is not locked and both would lock the resource and use it as it’s own. The solution to this is to have the entire block of code as synchronized. So only one thread is doing the check at one time, actually check as well as act. Only after the thread that got into the code completes it’s check and it’s act can another thread get to try. So, there is no room for misunderstanding between the threads now.
How is this piece of code an example of “CAS ( Compare and Swap)”?
The code expects that the value of locked is false and once that comparison succeeds, it swaps the value of locked with the value true.
What is AtomicBoolean?
AtomicBoolean is a class that was provided starting from Java 5 to address this need. There is no need to write a synchronized block with the check and act code any more. AtomicBoolean makes sure that only one thread can read it at a time.
private AtomicBoolean locked = new AtomicBoolean(false);
public boolean lock() {
return locked.compareAndSet(false, true);
}
This code checks whether the locked AtomicBoolean has the value false and it yes, sets it to true.
Get Learn’s stories in your inbox
Join Medium for free to get updates from this writer.
What is AtomicLong?
Long with thread-safe “Compare and Swap” functionality.
AtomicLong n = new AtomicLong(123);
long nValue = n.get();
System.out.println(nValue);
n.set(345);
n.compareAndSet(345, 500);
System.out.println(n.get());
n.compareAndSet(499, 600);
System.out.println(n.get());123
500 // compareAndSet with 345 passed and so the value was changed to 500
500// compareAndSet with 499 failed. So value was not changed to 600.
What is AtomicReference?
This provides an object reference variable which can be read and written atomically.
String c1 = “Red”;
String c2 = “Blue”;
AtomicReference<String> ref = new AtomicReference<>();
ref.set(c1);
System.out.println(ref.get());
ref.compareAndExchange(c1, c2);
System.out.println(ref.get());Red
Blue
When the compare fails, set does not happen as illustrated below.
String c1 = “Red”;
String c2 = “Blue”;
String c3 = “Yellow”;
AtomicReference<String> ref = new AtomicReference<>();
ref.set(c1);
System.out.println(ref.get());
ref.compareAndExchange(c1, c2);
System.out.println(ref.get());
ref.compareAndSet(c1, c3);
System.out.println(ref.get());Red
Blue
Blue
What is a Blocking queue?
Blocking queue is a queue that is thread safe to put into and take instances from.






