Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

volatile makes a field’s updates visible to other threads and orders those accesses; it does not make a compound operation such as count++ indivisible. Java atomic classes provide indivisible operations on a single value, such as incrementing a counter. Use volatile for simple shared state, an atomic class for single-value read-modify-write operations, and synchronization or another coordination mechanism when several values or steps must stay consistent.

What is the difference between atomic and volatile in Java?

volatile is a Java field modifier with memory-visibility and ordering guarantees. An atomic class, such as AtomicInteger, is an API for performing specific operations on a value atomically. The key distinction is that a volatile read or write applies to one access, while an atomic method can make a read-modify-write operation indivisible.

The Java Language Specification (Java SE 26) says that “A write to a volatile field happens-before every subsequent read of that field.” This relationship means that when one thread writes a volatile field and another subsequently reads that same field, the write and the actions ordered before it are visible according to the Java Memory Model. It is a defined memory-model guarantee, not simply a promise that the variable is always fetched from main memory. Java Language Specification, Chapter 17.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Does volatile make count++ thread-safe?

No. Even if count is volatile, count++ consists of multiple steps: read the current value, add one, and write the result. Two threads can read the same value before either writes, so one increment can overwrite the other. Volatile does not turn that sequence into one indivisible operation. Oracle’s Java concurrency tutorial.

volatile int count;

void increment() {
    count++; // Not an atomic read-modify-write operation
}

For an independent shared counter, use an atomic operation such as AtomicInteger.incrementAndGet() instead:

import java.util.concurrent.atomic.AtomicInteger;

AtomicInteger count = new AtomicInteger();
int newValue = count.incrementAndGet();

The increment method atomically updates that single value and returns the new value. Java’s atomic package also includes compare-and-set operations, which update a value only if it still matches an expected value. Java SE 26 atomic package documentation.

When should you use volatile, an atomic class, or synchronization?

Need Starting point Why
Publish a simple state or status flag for another thread volatile field A volatile write and a subsequent read of that field establish the specified visibility and ordering relationship.
Increment or conditionally update one shared value AtomicInteger, AtomicLong, AtomicReference, or another fitting atomic class Atomic operations support indivisible updates to a single value.
Keep multiple fields or steps consistent as one operation synchronized, locks, or a suitable higher-level concurrency abstraction A volatile access or single-variable atomic operation does not make a multi-step invariant indivisible.

How does volatile publish data to another thread?

A volatile field can serve as a handoff flag when one thread prepares data before setting the flag and another thread reads the flag before using that data. The volatile write/read relationship orders the earlier preparation before the reader’s subsequent actions.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
class Publication {
    private int value;
    private volatile boolean ready;

    void publish(int newValue) {
        value = newValue;
        ready = true;
    }

    int readWhenReady() {
        if (ready) {
            return value;
        }
        return -1;
    }
}

In this example, the flag is the handoff: when a reader observes the volatile write of true, the preceding write to value is ordered before the reader’s subsequent access. This pattern does not make arbitrary concurrent updates to value safe; it illustrates publication followed by reading.

Is a volatile variable atomic in Java?

It depends on what “atomic” means. A read or write of a volatile field is atomic as an individual access. The Java SE 26 specification specifically guarantees that reads and writes of volatile long and double values are always atomic. That does not mean a compound operation involving the field—such as incrementing it—is atomic. Java Language Specification, Chapter 17.

Do atomic classes make an object thread-safe?

No. An atomic class provides atomic operations on its own value; it does not automatically protect surrounding fields or a sequence of operations that must agree with one another. If correctness depends on a multi-field invariant—for example, changing a balance and a transaction count together—use synchronization, a lock, or a concurrency design that coordinates the whole operation.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Is volatile or atomic faster?

There is no universal speed ranking to apply. The choice should be based on the operation’s correctness requirements: visibility and ordering for a simple field, atomic update semantics for one value, or coordinated access for a broader invariant. The cited Java specification and API documentation define behavior; they do not establish a current quantified performance comparison.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.