CKTIS / Java
AtomicIntegerFieldUpdater — atomic updates to an int field
AtomicIntegerFieldUpdater provides atomic operations on an existing object's volatile int field. One updater can serve multiple instances of a class, without a separate AtomicInteger object for each field.
Version: Java SE 21; repository runtime, production runtime not attested
Syntax
AtomicIntegerFieldUpdater<T> updater = AtomicIntegerFieldUpdater.newUpdater(targetClass, "fieldName"); // targetClass is a Class<T>
int current = updater.get(obj);
boolean changed = updater.compareAndSet(obj, expect, update);AtomicIntegerFieldUpdater<T> in java.util.concurrent.atomic operates on a selected volatile int field of objects of type T. The value stays in the object, and the updater can be shared across instances. Such utilities support independent field updates in concurrent data structures.
AtomicIntegerFieldUpdater.newUpdater(targetClass, "fieldName") binds an updater to a field. The targetClass argument is a value of type Class<T>. The field must be a non-static volatile int and accessible to the code creating the updater under Java access rules. An invalid declaration, name, or access causes an exception during creation.
get and compareAndSet are instance methods called on the updater. updater.get(obj) reads the field. updater.compareAndSet(obj, expect, update) writes update only when the current value == expect; it returns true on success and false on mismatch. A separate read followed by a write does not replace this operation.
The atomicity guarantee covers calls to compareAndSet and set on the same updater, not arbitrary other field modifications. Therefore, avoid mixing independent update mechanisms for shared state. The single-threaded example demonstrates a successful and a rejected change; it is not a concurrency correctness test.
Examples
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
public class Main {
private volatile int state = 0;
private static final AtomicIntegerFieldUpdater<Main> STATE =
AtomicIntegerFieldUpdater.newUpdater(Main.class, "state");
public static void main(String[] args) {
Main item = new Main();
System.out.println(STATE.get(item));
System.out.println(STATE.compareAndSet(item, 0, 1));
System.out.println(STATE.compareAndSet(item, 0, 2));
System.out.println(STATE.get(item));
}
}Result
0 true false 1
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.