Atomic variable guarantees that the operations made on the variable occur in an atomic manner.
Atomic variables are classes, and these classes come from the package java.util.concurrent.atomic. Example: AtomicInteger, AtomicBoolean, AtomicLong, etc.
Example
import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.concurrent.atomic.AtomicInteger;
class Atomic_example implements Runnable
{
private AtomicInteger count=new AtomicInteger();
public void run()
{
for(int i=1;i<=5; i++)
{
try
{
Thread.sleep(i*50);
count.getAndIncrement();
}
catch (InterruptedException ex)
{
System.out.println(ex);
}
}
}
public int get_count()
{
return count.get();
}
}
public class Example
{
public static void main(String[] args) throws InterruptedException
{
Atomic_example r=new Atomic_example();
Thread th1 = new Thread(r);
th1.start();
Thread th2 = new Thread(r);
th2.start();
th1.join();
th2.join();
System.out.println("Count Value " + r.get_count());
}
}
You can also try this code with Online Java Compiler
It allows only a single thread at a time to access the shared resource. It will not allow other threads and make the other threads wait for that accessing thread to release its access to the shared resource.
Each object in Java has a unique lock. So when a thread wants to execute a shared resource, it has to obtain the lock on that object, thereby avoiding other threads to access the same object.
It can be applied to methods and blocks. It actually occurs on the object. For any given single object, only one thread can execute a synchronized method/block at a time.
Example
import java.util.*;
import java.lang.*;
import java.io.*;
class RaceCondition implements Runnable
{
private int count;
public void run()
{
for(int i=1;i<=5; i++)
{
try
{
Thread.sleep(i*50);
count++;
}
catch (InterruptedException ex)
{
System.out.println(ex);
}
}
}
public int get_count()
{
return count;
}
}
public class Example
{
public static void main(String[] args) throws InterruptedException
{
RaceCondition r=new RaceCondition();
Thread th1 = new Thread(r);
th1.start();
Thread th2 = new Thread(r);
th2.start();
th1.join();
th2.join();
System.out.println("Count Value " + r.get_count());
}
}
You can also try this code with Online Java Compiler
Difference between Atomic, Volatile, and Synchronized
Frequently Asked Questions
What is the use of atomic in Java? Ans: The atomic classes are used to provide a lock-free and thread-safe environment or programming on a single variable. It also supports atomic operations.
When can we use volatile variables in Java? Ans: We can use a volatile variable if we want to read and write long and double variables automatically.
Can we make the array volatile in Java? Ans: Yes, We can make an array (both primitive and reference type array, e.g., an int array and String array) volatile in Java.
Why do we need synchronization? Ans: The need for synchronization originates when processes need to execute concurrently. The main purpose of synchronization is to share resources without interference using mutual exclusion.
Key takeaways
In this article, we have extensively discussed the difference between Atomic, Volatile, and Synchronized in the java programming language. We also discussed Atomic, Volatile, and Synchronized keywords in Java with the help of an example.
We hope that this blog has helped you enhance your knowledge regarding the difference between Atomic, Volatile, and Synchronized, and if you would like to learn more, check out our article on Multiple Inheritance in Java. You can start learning java basics from scratch with our guided path.
Do upvote our blog to help other ninjas grow. Happy Coding!
Live masterclass
Crack GenAI Skills to ace 30 LPA+ roles at Amazon & Google
by Sumit Shukla
16 Feb, 2026
03:00 PM
Zero to Google Data Analyst: Roadmap for 30L+ CTC
by Prashant
15 Feb, 2026
06:30 AM
Beginner to GenAI Engineer Roadmap for 30L+ CTC at Amazon
by Shantanu Shubham
15 Feb, 2026
08:30 AM
Data Analysis for 20L+ CTC@Flipkart: End-Season Sales dataset
by Sumit Shukla
16 Feb, 2026
01:30 PM
Crack GenAI Skills to ace 30 LPA+ roles at Amazon & Google