Created
July 19, 2023 16:26
-
-
Save franz1981/f40d3e7b70fb881d401b63103dbd6c98 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package red.hat.puzzles.concurrent; | |
import org.openjdk.jmh.annotations.*; | |
import org.openjdk.jmh.infra.Blackhole; | |
import java.lang.invoke.MethodHandles; | |
import java.lang.invoke.VarHandle; | |
import java.util.concurrent.TimeUnit; | |
import java.util.concurrent.atomic.AtomicLongFieldUpdater; | |
@State(Scope.Benchmark) | |
@Warmup(iterations = 10, time = 400, timeUnit = TimeUnit.MILLISECONDS) | |
@Measurement(iterations = 5, time = 200, timeUnit = TimeUnit.MILLISECONDS) | |
@OutputTimeUnit(TimeUnit.NANOSECONDS) | |
@BenchmarkMode(Mode.AverageTime) | |
@Fork(jvmArgs = "-XX:LoopUnrollLimit=1", value = 2) | |
public class StoreOps { | |
private static final AtomicLongFieldUpdater<StoreOps> VALUE_UPDATER = AtomicLongFieldUpdater.newUpdater(StoreOps.class, "updValue"); | |
private static final VarHandle VALUE_HANDLE; | |
static { | |
try { | |
VALUE_HANDLE = MethodHandles.lookup().findVarHandle(StoreOps.class, "value", long.class); | |
} catch (ReflectiveOperationException e) { | |
throw new ExceptionInInitializerError(e); | |
} | |
} | |
// using int on purpose here | |
private static final int VALUE = 13; | |
private long value; | |
private volatile long updValue; | |
@Benchmark | |
public void updaterSetRelease() { | |
VALUE_UPDATER.lazySet(this, VALUE); | |
} | |
@Benchmark | |
public void vhSetRelease() { | |
VALUE_HANDLE.setRelease(this, VALUE); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
perfnorm
if very esplicit: