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
Example:
// Atomic Variable
AtomicInteger var;
Need for Atomic Variable
Consider the below example:
Java
class Counter extends Thread {
int count = 0 ;
public void run()
{
int max = 1_000_00_000;
for ( int i = 0 ; i < max; i++) {
count++;
}
}
}
public class UnSafeCounter {
public static void main(String[] args)
throws InterruptedException
{
Counter c = new Counter();
Thread first = new Thread(c, "First" );
Thread second = new Thread(c, "Second" );
first.start();
second.start();
first.join();
second.join();
System.out.println(c.count);
}
}
|
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.
Java
import java.io.*;
import java.util.concurrent.locks.*;
class Counter extends Thread {
int count = 0 ;
public synchronized void run()
{
int max = 1_000_00_000;
for ( int i = 0 ; i < max; i++) {
count++;
}
}
}
public class SynchronizedCounter {
public static void main(String[] args)
throws InterruptedException
{
Counter c = new Counter();
Thread first = new Thread(c, "First" );
Thread second = new Thread(c, "Second" );
first.start();
second.start();
first.join();
second.join();
System.out.println(c.count);
}
}
|
2. Using Atomic variable:
Java
import java.util.concurrent.atomic.AtomicInteger;
class Counter extends Thread {
AtomicInteger count;
Counter()
{
count = new AtomicInteger();
}
public void run()
{
int max = 1_000_00_000;
for ( int i = 0 ; i < max; i++) {
count.addAndGet( 1 );
}
}
}
public class AtomicCounter {
public static void main(String[] args)
throws InterruptedException
{
Counter c = new Counter();
Thread first = new Thread(c, "First" );
Thread second = new Thread(c, "Second" );
first.start();
second.start();
first.join();
second.join();
System.out.println(c.count);
}
}
|
Last Updated :
10 Jul, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...