In a multi-threaded environment, the most common problem we encounter is synchronizing the values of variables. Since variables need to be shared across multiple threads, we must need to employ some synchronization mechanism to control them.
From the previous article we know that the Lock mechanism can be used, and of course, the Atomic class we are talking about today.
Here we will introduce each of the two ways.
In the previous article, we also talked about synchronization, so let’s review it again. If a counter is defined as follows.
If it is in a single threaded environment, the above code has no problem. But in a multi-threaded environment, counter++ will give a different result.
Because although counter++ looks like an atomic operation, it actually contains three operations: read the data, add one, and write back the data.
Our previous article also talks about how to solve this problem.
By adding synchronized, it is guaranteed that only one thread will be reading and writing the counter variable at the same time.
By volatile, it is guaranteed that all data is directly manipulated in the main cache without using the thread cache.
This solves the problem, but performance may suffer because synchronized locks the entire LockCounter instance.
By introducing low-level atomic semantic commands (such as compare-and-swap (CAS)), atomicity can be guaranteed while maintaining efficiency.
A standard CAS consists of three operations.
- the memory address M to be manipulated.
- the existing variable A.
- the new variable B to be stored.
CAS will first compare the values stored in A and M. If they are the same, the variable is replaced with B if no other thread has modified it. Otherwise, no operation is performed.
Using CAS can be done without blocking other threads, but we need to handle our own business logic in case of update failure.
Java provides many Atomic classes, the most common ones include AtomicInteger, AtomicLong, AtomicBoolean, and AtomicReference.
The main methods are.
- get() - Reads the value of a variable directly from main memory, similar to a volatile variable.
- set() - Writes the variable back to main memory. Similar to volatile variables.
- lazySet() - Delays writing back to main memory. A common scenario is to reset a reference to null.
- compareAndSet() - Performs a CAS operation, returns true on success, false on failure.
- weakCompareAndSet() - A weaker CAS operation, the difference being that it does not perform a happens-before operation and thus is not guaranteed to read the latest values of other variables.
Let’s see how it works.