How do you use a VarHandle to perform atomic operations on array elements, and why is that useful?
answer
- MethodHandles.arrayElementVarHandle(T[].class)
- Coordinates are (array, index) at every call
- Per-element CAS/volatile without an Atomic wrapper per slot
- Underlies AtomicIntegerArray / AtomicReferenceArray
- Use for striped counters, concurrent-map buckets, ring buffers
basics
~20 sYou create an array-element VarHandle with MethodHandles.arrayElementVarHandle(int[].class). Then operations like getVolatile or compareAndSet take the array plus an index, letting you atomically update one slot. This is useful for lock-free arrays — like a striped counter or a hash table's bucket array — without wrapping every slot in an Atomic object.
solid answer
~40 sMethodHandles.arrayElementVarHandle(SomeType[].class) returns a VarHandle whose coordinates are (the array, the index). You then call get/set/getVolatile/setRelease/compareAndSet/getAndAdd passing the array and the element index, giving you per-element atomic and ordered access without an AtomicReferenceArray/AtomicIntegerArray wrapper or a separate Atomic object per slot. It is useful for building lock-free structures over a backing array — striped counters, bucket arrays in concurrent maps, ring buffers — where you want CAS on individual slots cheaply and with a chosen memory-ordering mode. The handle also enforces the array's component type at access time. Compared with AtomicIntegerArray (which is itself built on this mechanism and since Java 9 also has plain, opaque and acquire/release element methods), a raw VarHandle works in place on an array you already hold, where AtomicIntegerArray(int[]) copies it, and adds acquire/release getAndAdd and the bitwise atomics.
code
java · 28 linesimport java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
class StripedCounter {
private final long[] cells;
private static final VarHandle CELLS =
MethodHandles.arrayElementVarHandle(long[].class);
StripedCounter(int stripes) { this.cells = new long[stripes]; }
// Each caller hits a cell (e.g. by thread hash), so fewer CASes fail on one value.
// Adjacent long[] slots still share a cache line (false sharing); LongAdder pads its cells.
void add(int cell, long delta) {
long prev, next;
do {
prev = (long) CELLS.getVolatile(cells, cell);
next = prev + delta;
} while (!CELLS.compareAndSet(cells, cell, prev, next));
}
long sum() {
long total = 0;
for (int i = 0; i < cells.length; i++) {
total += (long) CELLS.getVolatile(cells, i);
}
return total;
}
}go deeper
Not expected; knowing arrays aren't thread-safe by default is sufficient at this level.
Can recall that AtomicIntegerArray/AtomicReferenceArray exist for atomic per-element updates; VarHandle array access is advanced.
Can create an array-element VarHandle, call CAS/volatile with the (array,index) coordinates, and explain its use in lock-free arrays.
Designs striped/lock-free array structures choosing per-operation access modes, understands the relation to AtomicIntegerArray and alignment constraints on view handles.
## The need Sometimes the thing you want to update atomically is **one element of an array**, not a standalone field. Classic cases: a **striped counter** (an array of cells updated by different threads to reduce contention — the idea behind `LongAdder`), the **bucket array** of a concurrent hash table, or slots of a **ring buffer** in a lock-free queue. You want to CAS or volatile-read a single slot, with as little overhead as possible, and ideally with control over memory ordering. Java's older answer was wrapper classes like `AtomicIntegerArray` and `AtomicReferenceArray` — objects that wrap an array and expose `get(i)`, `set(i, v)`, `compareAndSet(i, exp, new)`. They work, and since Java 9 they also offer plain, opaque and acquire/release element methods (`getAcquire(i)`, `setRelease(i, v)`), but they add a wrapper object, and the `AtomicIntegerArray(int[])` constructor copies the array. ## The VarHandle way `java.lang.invoke.MethodHandles` provides a factory specifically for array elements: ```java VarHandle AH = MethodHandles.arrayElementVarHandle(int[].class); ``` The returned VarHandle has **two coordinate types**: the array itself and an `int` index. So every access takes those plus any operands: ```java int[] arr = new int[16]; int old = (int) AH.getVolatile(arr, 3); // volatile read of arr[3] AH.setRelease(arr, 3, 42); // release write of arr[3] boolean ok = AH.compareAndSet(arr, 3, 42, 43); // CAS on arr[3] int prev = (int) AH.getAndAdd(arr, 3, 5); // atomic add to arr[3] ``` Key points: - **No per-slot wrapper.** You operate directly on the primitive (or reference) elements of an ordinary array — no `Atomic*` object per slot and no `AtomicIntegerArray` wrapper. - **Full access-mode ladder.** Just like field VarHandles, array-element handles support plain/opaque/acquire-release/volatile reads and writes, plus `compareAndSet`, `weakCompareAndSet`, `getAndAdd`, etc. You pick the ordering strength per operation. - **Type and bounds safety.** The component type is fixed when you create the handle (`int[].class` here), and element access is bounds-checked like normal array access, so you keep Java's safety — unlike raw offset arithmetic in `sun.misc.Unsafe`. - **One handle for the whole array type.** A single `static final` VarHandle works for any array of that component type; you pass the specific array and index at call time. ## Reference arrays and the contended case For object arrays you create the handle over the reference array type, e.g. `MethodHandles.arrayElementVarHandle(Node[].class)`, and CAS references into slots — exactly what a concurrent hash map does when installing a node into a bucket. For **highly contended counters**, striping updates across many array cells (each thread mostly hits its own cell) reduces failed CASes on any one value. It does not by itself stop cache-line contention: adjacent slots of a `long[]` share a cache line, so threads on neighbouring cells still bounce it (false sharing). `LongAdder` stripes too, but over separate `Cell` objects padded with `@Contended` so no two share a line. ## Why prefer it over AtomicIntegerArray `AtomicIntegerArray` is actually implemented on top of this same machinery. Using the VarHandle directly buys you: (1) the **modes the wrapper lacks**, such as acquire/release `getAndAdd` and `getAndSet` and the bitwise atomics (`getAndBitwiseOr` and friends), since both already share plain, opaque, acquire/release and volatile reads and writes, (2) no wrapper allocation and no copy if you already have the array, and (3) uniform code with your field VarHandles. The cost is that it is lower-level and easier to misuse, so it belongs in carefully written data-structure code rather than everyday logic. ## A subtlety: alignment and byte-buffer views Beyond plain element arrays, related factories (`byteArrayViewVarHandle`, `byteBufferViewVarHandle`) let you read/write multi-byte values out of a `byte[]`/`ByteBuffer` with a chosen endianness. Since JDK 23 (the same on 25 and 27) they allow only plain `get` and `set` on a `byte[]` view or a heap buffer: any other mode throws (`UnsupportedOperationException` for the array view, `IllegalStateException` for a heap buffer). Only a **direct** buffer keeps the atomic modes, and only at an **aligned** index; before JDK 23 aligned atomic access on `byte[]` views worked too. For atomic access to raw bytes, the JDK points to direct buffers or a `MemorySegment`. That's an advanced corner; the core array-element handle is the common tool. ## Summary An array-element VarHandle (from `MethodHandles.arrayElementVarHandle`) has coordinates (array, index) and lets you perform the full ladder of atomic/ordered operations on individual array slots — no per-slot Atomic wrapper, with type and bounds safety. It underlies `AtomicIntegerArray`/`AtomicReferenceArray` and is the tool for lock-free arrays like striped counters, concurrent-map bucket arrays, and ring buffers, working in place on arrays you already hold.
- What advantage does a raw array-element VarHandle have over AtomicIntegerArray?It works in place on an array you already hold (AtomicIntegerArray(int[]) copies it into a wrapper), one static handle serves every array of that type, and it adds modes the wrapper lacks, such as acquire/release getAndAdd and the bitwise atomics. Both have plain, opaque, acquire/release and volatile reads and writes since Java 9.
- How does an array-element VarHandle relate to building a LongAdder-style striped counter?You can back the counter with a long[] and CAS cells through the handle, so threads on different cells stop failing each other's CAS. It is not LongAdder's layout, though: adjacent long[] slots share a cache line, so neighbouring cells still contend (false sharing). LongAdder keeps separate Cell objects padded with @Contended.
- Thinking you need an Atomic object per array slot — the handle works on raw elements
- Forgetting to pass the index coordinate to each access
- Assuming array VarHandles skip bounds checks — element access is still bounds-checked
- Believing byte[]-view VarHandles support atomic modes — since JDK 23 only direct buffers do, and only aligned
- Claiming AtomicIntegerArray is unrelated — it is built on the same mechanism
- VarHandle for array elements
- Java 9
- Atomic modes on byte-array and heap-buffer view handles
- Java 9removed 23
Assumes JDK 27 and checks 25: both allow only plain get and set on byte[] view handles and heap byte buffers, a restriction JDK 23 introduced; before 23, aligned atomic access on those views worked.
context
- MethodHandles (Java SE 27 & JDK 27)docs.oracle.com
- AtomicIntegerArray (Java SE 27 & JDK 27)docs.oracle.com