Atomic Variables in Java with Examples
In multithreading, the shared entity mostly leads to a problem when concurrency is incorporated. A shared entity such as, mutable object or variable, might be changed, which may result in the inconsistency of the program or database. So, it becomes crucial to deal with the shared entity while accessed concurrently. An atomic variable can be one of the alternatives in such a scenario.
Java provides atomic classes such as AtomicInteger, AtomicLong, AtomicBoolean and AtomicReference. Objects of these classes represent the atomic variable of int, long, boolean, and object reference respectively. These classes contain the following methods.
- set(int value): Sets to the given value
- get(): Gets the current value
- lazySet(int value): Eventually sets to the given value
- compareAndSet(int expect, int update): Atomically sets the value to the given updated value if the current value == the expected value
- addAndGet(int delta): Atomically adds the given value to the current value
- decrementAndGet(): Atomically decrements by one the current value
// Atomic Variable AtomicInteger var;
Need for Atomic Variable
Consider the below example:
In a single thread environment, the above-mentioned class will give the expected result only. But when a multithreaded environment is concerned, it may lead to inconsistent results. It happens because updating “var” is done in three steps: Reading, updating, and writing. If two or more threads try to update the value at the same time, then it may not update properly.
This issue can be solved using Lock and Synchronization, but not efficiently.
1. Using lock analogy or synchronization: Synchronization or Locking can solve our problem, but it compromises time efficiency or performance. First, it mandates resource and thread scheduler to control lock. Second, when multiple threads attempt to acquire a lock, only one of them wins, rest are suspended or blocked. Suspending or blocking of threads can have a huge impact on performance.
2. Using Atomic variable:
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.