Benchmark keeps running
Aleksey Shipilev
aleksey.shipilev at oracle.com
Wed May 8 00:17:39 PDT 2013
On 05/08/2013 07:21 AM, Yann Le Tallec wrote:
> @OutputTimeUnit(TimeUnit.NANOSECONDS)
> public class CAS {
>
> @State(Scope.Benchmark)
> public static class AIState {
> private final AtomicInteger ai = new AtomicInteger();
> }
>
> @GenerateMicroBenchmark(BenchmarkType.AverageTimePerOp)
> public int cas(CAS.AIState state) {
> int i = state.ai.get();
> while (!state.ai.compareAndSet(i, i + 1)) { }
> return state.ai.get();
> }
> }
Your benchmark is probably incorrect, you have to re-read the AI value
in the CAS'ed loop: if you had failed to CAS, that means the current
value is not "i" anymore:
@GenerateMicroBenchmark(BenchmarkType.AverageTimePerOp)
public int cas(AIState state) {
int i;
while (!state.ai.compareAndSet((i = state.ai.get()), i + 1)) { }
return state.ai.get();
}
Otherwise some thread can obviously be stuck. If that is the behavior
you are after, you have to let JMH terminate the loop, see
http://hg.openjdk.java.net/code-tools/jmh/file/tip/jmh-samples/src/main/java/org/openjdk/jmh/samples/JMHSample_18_Control.java
-Aleksey.
More information about the jmh-dev
mailing list