Hackforge Academy

Category: java

Introduction to Atomic Variables

Published on 27 Oct 2026

Explanation

Classes such as AtomicInteger and AtomicLong provide thread-safe atomic operations without requiring explicit synchronized blocks for each operation. They are useful for counters, sequence numbers, and independent state updates.

Code:

import java.util.concurrent.atomic.AtomicInteger;

public class AtomicIntegerDemo {
    public static void main(String[] args) {
        AtomicInteger count = new AtomicInteger(0);
        count.incrementAndGet();
        System.out.println(count.get());
    }
}

Explanation

Both methods atomically increment an AtomicInteger. incrementAndGet() returns the updated value, while getAndIncrement() returns the previous value. Choose based on which value the calling code needs.

Code:

import java.util.concurrent.atomic.AtomicInteger;

public class AtomicMethodsDemo {
    public static void main(String[] args) {
        AtomicInteger n = new AtomicInteger(5);
        System.out.println(n.incrementAndGet()); // 6
        System.out.println(n.getAndIncrement()); // 6
        System.out.println(n.get()); // 7
    }
}

Explanation

compareAndSet(expected, update) changes a value only if it currently equals the expected value. It is a building block for lock-free algorithms and conditional state updates.

Code:

import java.util.concurrent.atomic.AtomicInteger;

public class CasDemo {
    public static void main(String[] args) {
        AtomicInteger state = new AtomicInteger(0);
        boolean changed = state.compareAndSet(0, 1);
        System.out.println(changed);
        System.out.println(state.get());
    }
}

Explanation

AtomicReference provides atomic operations on an object reference. It can safely replace a reference conditionally, but it does not make the referenced object's internal mutable fields automatically thread-safe.

Code:

import java.util.concurrent.atomic.AtomicReference;

public class AtomicReferenceDemo {
    public static void main(String[] args) {
        AtomicReference<String> status = new AtomicReference<>("NEW");
        status.compareAndSet("NEW", "PROCESSING");
        System.out.println(status.get());
    }
}

Explanation

Atomic classes are convenient for independent atomic updates and often use compare-and-set internally. synchronized is better when several fields or steps must be protected together as one invariant.

Code:

import java.util.concurrent.atomic.AtomicInteger;

public class AtomicVsLockDemo {
    private final AtomicInteger requests = new AtomicInteger();
    public int recordRequest() { return requests.incrementAndGet(); }
    public int totalRequests() { return requests.get(); }
}

πŸš€ Learn Spring Boot with real-world projects

πŸ’‘ Build REST APIs step by step

🧠 Improve backend development skills

🎯 Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

🐍

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
βš›οΈ

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
β˜•

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community