Scrivio Interview library
ConceptPrincipal7 min read · updated 12 November 2026

Java 9 added VarHandle access modes (plain, opaque, acquire/release, volatile) that resemble the memory orders of C++11. Why did Java expose these weaker modes, and should application teams in your organisation use them?

Java exposed VarHandle's plain, opaque, acquire/release and volatile modes because the JDK and high-performance libraries needed fine-grained memory ordering without relying on sun.misc.Unsafe, and because weaker modes can avoid full fences on some hardware in very hot paths. They also make correctness reasoning much harder and are easy to get subtly wrong, especially on weakly ordered CPUs such as ARM. So application teams should use volatile, atomics and java.util.concurrent, and weaker modes should live only in reviewed library code backed by measurements and jcstress tests.

Why this question is asked

Java 9 quietly gave Java programmers something close to the memory orders of C++11: a way to say, for one access, exactly how much ordering it needs. For a principal engineer this is a governance question as much as a technical one. Somebody on a team will read about getAcquire and setRelease, measure a speedup in a benchmark, and want to use them. Interviewers ask this to see whether we know why the feature exists, what it can buy, what it costs, and where we would draw the line in an organisation. In this answer, we will go back to the problem VarHandle solved, compare its modes with C++ memory orders, look at where the speedup comes from and what can go wrong, and then set a policy for application teams.

1The Problem: Libraries Living on sun.misc.Unsafe

Before Java 9, the public tools for memory ordering were volatile fields, synchronized, and the atomic classes such as AtomicInteger. These all give strong ordering. Library authors who needed more control, such as compare-and-set on a plain field without wrapping it in an AtomicInteger, an ordered write without a full fence (lazySet), or atomic access to array elements, used sun.misc.Unsafe, an internal class that offered raw memory operations. The JDK's own java.util.concurrent, and libraries such as Netty, Disruptor, Caffeine and JCTools, depended on it.

Unsafe was never a supported API. It could crash the JVM with a wrong offset, it had no defined memory semantics beyond what implementations happened to do, and the module system in Java 9 aimed to hide JDK internals. VarHandle, from JEP 193, is the supported replacement: a typed, checked handle to a field, array element or buffer location, with a defined set of access modes. Unsafe's memory access methods have since been deprecated for removal in recent JDK versions, which makes VarHandle the long-term path for such libraries; check your JDK's status for the details.

2The Modes and Their C++11 Counterparts

The modes form a ladder. Each step adds guarantees and, on some hardware, cost.

VarHandle modes and C++11 memory orders
VarHandle modeGuaranteesClose C++11 order
Plain (get/set)None across threads; may be reordered or hoistedNon-atomic access
OpaqueAccess happens, coherent per variable, orders nothing elsememory_order_relaxed
Acquire / releasegetAcquire sees writes before the setRelease it readsacquire / release
VolatileTotal order of all volatile accesses, incl. StoreLoadmemory_order_seq_cst

The resemblance to C++ is deliberate: both build on the same research on memory models and the same hardware realities. One notable difference is that Java has no equivalent of C++ consume ordering, and that plain accesses in Java still keep some safety for racy programs, such as no out-of-thin-air values for references, while racy plain accesses in C++ are undefined behaviour.

3Where Weaker Modes Help

The gain comes from avoiding barriers the algorithm does not need. A volatile write on x86 is followed by a StoreLoad barrier, typically a locked instruction that drains the store buffer. A setRelease write on x86 is a plain store. In a single-producer queue, such as the ones in JCTools or the Disruptor, the producer publishes each slot with a release write and the consumer reads it with an acquire read; removing the full fence from every enqueue can raise throughput noticeably in microbenchmarks. On ARM, acquire and release map to dedicated instructions, and opaque accesses can avoid ordering instructions entirely.

These gains are real in code that runs hundreds of millions of times per second on a hot path: the core of a message queue, a metrics library, a concurrent collection. In typical application code, where a request spends milliseconds on I/O, the difference between a volatile and a release write is invisible.

Java
// One-way publication in a single-producer ring buffer slot
SLOT.setRelease(buffer, index, event); // producer: event fully built before this
Event e = (Event) SLOT.getAcquire(buffer, index); // consumer: sees event's fields
// The same with volatile semantics: correct, slightly more expensive writes on x86
SLOT.setVolatile(buffer, index, event);

4Why They Are Easy to Get Wrong

The weaker the mode, the more the correctness of a program depends on reasoning that no tool checks. Release/acquire does not give a single total order: two threads that each write one variable with setRelease and then read the other with getAcquire can both see the old values, an outcome volatile forbids. Code that seems to work on x86 can break on ARM, because x86 hardware performs almost none of the reorderings that weaker modes allow, so tests on developer machines and x86 CI prove very little. And a later, innocent-looking change, such as adding a second flag, can silently invalidate the original argument.

The failures are rare, timing-dependent and often show up as corrupted data rather than exceptions. For a whole organisation, that makes weaker modes a source of bugs that are very expensive to find.

Step throughTwo threads, release/acquire, both read old values
Thread 1x and yThread 2
X.setRelease(1)
Y.setRelease(1)
Y.getAcquire() -> 0
X.getAcquire() -> 0
allowed: no StoreLoad ordering

5 messages in order. Play them, or step through them one at a time.

With volatile on both, at least one thread must see 1.

5A Policy for Application Teams

The policy I would set is short. Application teams use volatile fields, the atomic classes, locks and java.util.concurrent; they do not use VarHandle access modes weaker than volatile. A small set of reviewed platform libraries may use them, under three conditions: a measurement that shows the gain at the level that matters, a written argument for each access explaining why the chosen mode is enough, and jcstress tests for the critical interactions, run in CI on both x86 and ARM. Code review for those modules requires one of the few people trained for it.

We can enforce the first part with an ArchUnit test or a lint rule that flags calls to getAcquire, setRelease, getOpaque, setOpaque and plain get/set on VarHandle outside the approved packages. Before writing such code at all, teams should check whether an existing library, such as JCTools queues or Caffeine, already provides the structure, since those projects have done the reasoning and testing already.

6Follow-up questions

Is it wrong to use VarHandle in application code at all?

Not always. Using a VarHandle with volatile or compareAndSet semantics, for example to avoid an AtomicReference object per instance in a class with millions of instances, is reasonable and easy to reason about. The risk is in the weaker modes, so the policy targets them rather than VarHandle itself.

What does lazySet on the atomic classes have to do with this?

lazySet, added in Java 6, was the first public ordered write weaker than volatile; it is the same as setRelease. Since Java 9 the atomic classes also offer setRelease, getAcquire, setOpaque and getOpaque directly, which are built on the same mechanisms as VarHandle. The same policy should cover these methods.

How would you convince a team that has measured a gain?

I would ask where the gain appears. If it is only in a microbenchmark, we measure it in the service under realistic load. If it holds there, the work moves into a reviewed library module with the argument and jcstress tests, rather than staying in application code. That keeps the gain and moves the risk to where it can be controlled.

7Conclusion

In this answer, we saw that VarHandle's access modes replaced sun.misc.Unsafe for library authors and gave Java memory orders similar to C++11, so hot paths can avoid fences they do not need. We also saw why the weaker modes are dangerous: they remove ordering guarantees in ways that tests on x86 rarely reveal and later changes can silently break. The policy that follows is to keep application teams on volatile, atomics and java.util.concurrent, and to allow weaker modes only in reviewed library code with measurements and jcstress tests on both x86 and ARM.

What to say in the interviewAbout 90 seconds aloud

Open with

Java added VarHandle access modes mainly for library authors. Before Java 9, the JDK's own concurrency classes and libraries like Netty, Disruptor and Caffeine needed things like ordered writes without a full fence, compare-and-set on plain fields and atomic array access, and they got them from sun.misc.Unsafe, which was unsupported, unsafe and being hidden by modules.

Then, if they want more

VarHandle gave them a supported, typed API with defined modes: plain, opaque, acquire and release, and volatile, roughly matching C++11's non-atomic, relaxed, acquire and release, and sequentially consistent orders. The benefit is performance in very hot paths.

A volatile write on x86 needs a StoreLoad barrier, while a release write is a plain store, so a single-producer queue that publishes with setRelease and reads with getAcquire can be noticeably faster. The cost is that correctness depends on subtle reasoning.

Release and acquire give no total order, so patterns that are correct with volatile can fail, and because x86 hardware hides most reorderings, the bugs often only appear on ARM, under load, as corrupted data.

For application teams, my answer is no: they should use volatile, atomics and java.util.concurrent, and we would flag weaker modes with an ArchUnit or lint rule outside approved packages.

Weaker modes belong in a few reviewed platform libraries, and only with a measurement showing the gain matters, a written argument per access, and jcstress tests running on both x86 and ARM in CI.

About this answer

Last updated
12 November 2026
Versions it names
Java 9, Java 6
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
JLS chapter 17, Threads and Locks
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Java 9 added VarHandle access modes (plain, opaque, acquire/release, volatile) that...": https://scrivio-interview-library.netlify.app/java/java-memory-model/09f25808e3/