How do VarHandle's access modes (plain, opaque, acquire/release and volatile) differ in memory-ordering strength?
answer
- Strength ladder: plain < opaque < acquire/release < volatile
- Opaque = atomic + per-variable coherence, no cross-var order
- Acquire/release = one-directional publish→consume ordering
- Volatile = full bidirectional, globally ordered, most expensive
- weakCompareAndSet may fail spuriously → use in a loop; pick weakest correct mode
basics
~20 sVarHandle lets each access pick its ordering strength: plain gives no cross-thread ordering, opaque makes one variable's accesses atomic and coherent, release/acquire publish earlier writes to a matching reader, and volatile is totally ordered. Use the weakest mode that is still correct.
solid answer
~50 sVarHandle exposes a ladder of access modes that trade ordering strength for performance. Plain (get/set) is an ordinary read/write with no inter-thread ordering or atomicity guarantee beyond the language baseline. Opaque (getOpaque/setOpaque) guarantees the access is atomic and that accesses to the same variable are coherently ordered, but imposes no ordering relative to other variables. Acquire/release (getAcquire/setRelease) gives one-directional ordering: in the Javadoc's words, acquire reads and their subsequent accesses are ordered after matching release writes and their previous accesses — the cheap publication idiom. Volatile (getVolatile/setVolatile) is the strongest: full bidirectional ordering equivalent to a volatile field, with a global ordering of volatile accesses. compareAndSet is volatile-strength only; compareAndExchange, getAndSet, getAndAdd and the bitwise ops add Acquire and Release forms, and weakCompareAndSet adds Plain, Acquire and Release. The engineering point: use the weakest mode that remains correct, because weaker modes permit more hardware/compiler reordering and run faster on relaxed-memory CPUs.
code
java · 40 linesimport java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
class Publication {
private int payload; // written plain
private volatile boolean ready;
private static final VarHandle READY;
static {
try {
READY = MethodHandles.lookup()
.findVarHandle(Publication.class, "ready", boolean.class);
} catch (ReflectiveOperationException e) {
throw new ExceptionInInitializerError(e);
}
}
void produce(int v) {
payload = v; // plain store of the data
READY.setRelease(this, true); // release: publishes payload + flag
}
int consume() {
// acquire: if we see the release, we see everything before it
if ((boolean) READY.getAcquire(this)) {
return payload; // safe: ordered after the release
}
return -1;
}
// weakCompareAndSet may fail spuriously, so retry while the flag is still false
boolean setReadyViaCas() {
while (!(boolean) READY.getVolatile(this)) {
if (READY.weakCompareAndSet(this, false, true)) {
return true; // this call set the flag
}
// false: spurious failure, or another thread set it; re-check
}
return false; // someone else had already set it
}
}go deeper
Not expected to know access modes; knowing volatile exists and gives visibility is enough at this level.
Can state that VarHandle offers stronger and weaker access modes and that volatile is the strongest; details of opaque/acquire-release are aspirational.
Explains the ladder, what acquire/release publication buys over volatile, and that weakCompareAndSet can fail spuriously, so code that needs the update loops.
Reasons precisely about each mode's guarantees, maps them to hardware barriers and the JMM, and applies 'weakest correct mode' to design or review a lock-free primitive.
## Why memory ordering is even a thing Modern CPUs and compilers **reorder** memory operations and keep values in per-core caches/registers for speed. Without rules, a write by thread A may become visible to thread B late, or out of program order. The **Java Memory Model (JMM)** defines, via the **happens-before** relation, when one thread's writes are guaranteed visible to another. A **memory barrier** (or fence) is the hardware/compiler mechanism that restricts reordering to enforce such guarantees — but barriers cost performance. The art of lock-free programming is using the **cheapest barrier that is still correct**. Before VarHandle, your field-ordering choices in pure Java were coarse: a field was either plain or `volatile` (all-or-nothing strong ordering). VarHandle exposes a **ladder of access modes** so you can ask for exactly the strength you need. ## The ladder, weakest to strongest ### 1. Plain — `get` / `set` Behaves like a normal field read/write. The only guarantee is the JMM baseline: no tearing for most types, but **no inter-thread ordering and no visibility guarantee** — another thread might never see the write, or see it reordered. Use when the variable is thread-confined or ordering is established by something else. (Note: even plain access to a `long`/`double` is not guaranteed atomic unless the variable is otherwise made so; opaque and stronger guarantee atomicity.) ### 2. Opaque — `getOpaque` / `setOpaque` Adds two things over plain: **bitwise atomicity** for every type, `long` and `double` included (plain guarantees it only for references and values of at most 32 bits), and **per-variable coherence** — accesses to *that same variable* are coherently ordered. What opaque does **not** give is any ordering relative to *other* variables: opaque writes to different variables can be observed out of order. Think "this one variable behaves sanely, but don't rely on it to publish anything else." It suits a flag or counter that is read on its own, with nothing else published through it. ### 3. Acquire / Release — `getAcquire` / `setRelease` This is the **one-directional (half) barrier** pair, the workhorse of efficient lock-free code. - A **`setRelease(x)`** ensures that **all writes the thread did before it** are visible to any thread that later does a `getAcquire` and observes `x`. (Release = "everything before me is published with me.") - A **`getAcquire`** ensures that **reads the thread does after it** see the published state, and are not hoisted above the acquire. (Acquire = "everything after me sees what the matching release published.") Together they order accesses **only in the publish→consume direction**, which is cheaper than full volatile because each constrains reordering in just one direction. JEP 193 built these modes without revising the Java Memory Model, so the Javadoc states them as ordering rules, not JLS happens-before edges. The classic *safe publication* idiom: write the payload with plain stores, then `setRelease` a flag/reference; the reader `getAcquire`s the flag and then safely reads the payload. ### 4. Volatile — `getVolatile` / `setVolatile` The **strongest** mode, equivalent to reading/writing a `volatile` field. It gives full **bidirectional** ordering at the access and participates in a **total order of all volatile (sequentially-consistent) accesses**, so independent volatile variables can't be observed in inconsistent orders. This is what you need when you require global ordering between *different* variables. It is also the most expensive (full fences on relaxed architectures). ## Atomic operations and their modes The read-modify-write atomics also come in ordering flavors: - `compareAndSet` — full **volatile-strength** ordering (the default "strong CAS"). - `compareAndExchange` — returns the witnessed value; has `Acquire`/`Release` variants for one-directional ordering. - `weakCompareAndSet` — may **fail spuriously** even when the value matches, so a caller that needs the update retries it in a **loop** (or falls back to another path, as `LongAdder` does); in exchange it can be cheaper on some hardware. There are plain/acquire/release variants too. - `getAndAdd`, `getAndSet`, `getAndBitwiseOr`, etc., similarly default to volatile-strength with weaker variants. "Spurious failure" matters: a `false` from `weakCompareAndSet` does **not** mean the expected value was wrong, so code that needs the update must retry. ## The mental model and the rule Ordering strength: **plain < opaque < acquire/release < volatile**. Each step up adds guarantees and adds cost. The principle for experts: **choose the weakest access mode that keeps your algorithm correct.** Over-using `volatile`-strength is correct but slow on weakly-ordered CPUs (ARM, POWER); using too weak a mode is a subtle, rare, hard-to-reproduce data race. This is genuinely hard to reason about — which is why these modes are reserved for carefully reviewed concurrency primitives, not everyday code. ## A concrete publication example ```java class Holder { private int data; // plain payload private boolean ready; // published via release/acquire private static final VarHandle READY; static { try { READY = MethodHandles.lookup() .findVarHandle(Holder.class, "ready", boolean.class); } catch (ReflectiveOperationException e) { throw new Error(e); } } void publish(int v) { data = v; // 1: plain write of payload READY.setRelease(this, true); // 2: release — publishes (1) } int consume() { if ((boolean) READY.getAcquire(this)) // acquire — pairs with release return data; // guaranteed to see the published payload return -1; } } ``` Here acquire/release gives exactly the visibility needed without the heavier full-volatile fences. ## Summary VarHandle's access modes form a strength ladder — **plain** (no ordering), **opaque** (atomic + per-variable coherence), **acquire/release** (one-directional publish→consume ordering), and **volatile** (full bidirectional, globally ordered). They let an expert request precisely the barrier strength an algorithm needs, with the guiding rule to pick the weakest correct mode for performance, plus matching ordering variants on the atomic read-modify-write operations.
- When would you choose acquire/release over full volatile access?For the publish/consume idiom — write a payload, then setRelease a flag/reference; a reader getAcquires the flag and then reads the payload. You only need ordering in the publish→consume direction, so the cheaper one-directional barriers suffice and avoid full-volatile fence cost on weakly-ordered hardware.
- Why is weakCompareAndSet usually called inside a loop?It is allowed to fail spuriously, returning false even when the variable holds the expected value, which lets it map efficiently onto load-linked/store-conditional hardware. A false result does not imply a real mismatch, so code that needs the update retries until it succeeds or re-reads the value and finds a real change.
- Saying acquire/release gives full bidirectional ordering — it is one-directional
- Treating opaque as establishing ordering between different variables — it does not
- Claiming weakCompareAndSet's false return means the expected value was wrong — it can be spurious
- Believing plain access guarantees cross-thread visibility — it does not
- Assuming stronger modes are always 'safer to just use' without acknowledging the performance cost on relaxed CPUs
- VarHandle access modes
- Java 9
context
- VarHandle (Java SE 27 & JDK 27)docs.oracle.com
- JEP 193: Variable Handlesopenjdk.org