VOOZH about

URL: https://www.geeksforgeeks.org/java/atomic-variables-in-java-with-examples/

⇱ Atomic Variables in Java with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Atomic Variables in Java with Examples

Last Updated : 24 Apr, 2026

Atomic is a type of variable that performs read, write and update in a single uninterruptible step, ensuring thread-safe operations and preventing race conditions

  • It ensures data consistency without using synchronization or locks.
  • It improves performance through non-blocking, lock-free operations.
  • Simplify thread-safe programming for common operations like increment and compare-and-set.

Example: AtomicInteger in Multi-threading


Output
200000000

Explanation:

  • AtomicInteger ensures increments happen atomically (no interference between threads).
  • addAndGet(1) replaces count++, making it thread-safe.
  • This removes race conditions and guarantees the correct final count even with multiple threads.

Example: Without Atomic Variables (Unsafe Code)


Output
100238993

Explanation:

  • Both threads (first and second) update the same count variable without synchronization.
  • Since count++ is not atomic (it involves read, increment, write), multiple threads may interleave, causing incorrect results.
  • You expect count = 2 * max, but the final value will usually be less because increments get lost.

Types of Atomic Variables in Java

Below are some commonly used atomic variable types in Java.

👁 types_of_atomic_variables
Types of Atomic Variables

1. AtomicInteger

AtomicInteger provides atomic operations (increment, decrement, add, etc.) on integer values without synchronization.


Output
Final Value: 9

2. AtomicLong

AtomicLong supports atomic operations on long values for thread-safe numeric computations.


Output
Updated Value: 150

3. AtomicBoolean

AtomicBoolean represents a boolean value that can be atomically updated for safe flag-based operations.


Output
Operation performed only once!

4. AtomicReference

AtomicReference allows atomic read and write of object references, useful for non-primitive data.


Output
Current Message: Hi, from AtomicReference!

5. AtomicIntegerArray

AtomicIntegerArray enables atomic operations on elements of an integer array for thread-safe updates.


Output
Array after update: [1, 3, 3]
Value at index 1: 3

Commonly used methods in Atomic classes

MethodDescription
get()Returns the current value.
set(value)Sets the value to the given value.
getAndSet(value)Atomically sets a new value and returns the old one.
incrementAndGet()Increments the value by one and returns the updated value.
getAndIncrement()Increments the value by one and returns the previous value.
decrementAndGet()Decrements the value by one and returns the updated value.
addAndGet(delta)Adds the specified value and returns the updated result.
getAndAdd(delta)Adds the specified value and returns the previous result.
compareAndSet(expected, update)Atomically updates the value only if it equals the expected value.
lazySet(value)Eventually sets the value (may be delayed for performance).
Comment