New posts

This commit is contained in:
2026-11-06 21:43:48 +05:30
parent ecceaa8b32
commit f7b6c1efca
33 changed files with 1258 additions and 0 deletions
@@ -0,0 +1,17 @@
import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
/** AFTER: the same field with a VarHandle (JEP 193, JDK 9). No Unsafe anywhere. */
public class VarHandleFieldCounter implements FieldCounter {
private static final VarHandle VALUE;
static {
try { VALUE = MethodHandles.lookup().findVarHandle(VarHandleFieldCounter.class, "value", long.class); }
catch (ReflectiveOperationException e) { throw new ExceptionInInitializerError(e); }
}
private volatile long value;
public long get() { return value; }
public void set(long v) { value = v; }
public boolean cas(long expect, long update) { return VALUE.compareAndSet(this, expect, update); }
public long getAndAdd(long delta) { return (long) VALUE.getAndAdd(this, delta); }
}