Skip to content

Instantly share code, notes, and snippets.

@shipilev
Created December 28, 2016 10:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shipilev/0ff8caaae73ef32ccebd7b41c0dba353 to your computer and use it in GitHub Desktop.
Save shipilev/0ff8caaae73ef32ccebd7b41c0dba353 to your computer and use it in GitHub Desktop.
package org.openjdk;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLongFieldUpdater;
/**
* <code>
* -XX:BiasedLockingStartupDelay=0 -XX:+UseBiasedLocking
* </code>
*/
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Fork(1)
@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 10, time = 1, timeUnit = TimeUnit.SECONDS)
@Threads(1)
@State(Scope.Benchmark)
public class SinokTiChtoKuril {
public static class Counter {
private long count = 0L;
public synchronized long incAndGet() {
return ++count;
}
}
Counter[] etoPacaniKurili;
Counter yaProstoRyadomStoyal;
@Param({"false", "true"})
boolean pacaniKurili;
@Setup
public void setup() throws InterruptedException {
yaProstoRyadomStoyal = new Counter();
final int SIZE = 1000;
etoPacaniKurili = new Counter[SIZE];
for (int c = 0; c < SIZE; c++) {
etoPacaniKurili[c] = new Counter();
}
if (pacaniKurili) {
int threads = Runtime.getRuntime().availableProcessors();
ExecutorService executor = Executors.newFixedThreadPool(threads);
for (int t = 0; t < threads; t++) {
executor.submit(() -> {
for (int c = 0; c < 100000; c++) {
etoPacaniKurili[ThreadLocalRandom.current().nextInt(SIZE)].incAndGet();
}
});
}
executor.shutdown();
executor.awaitTermination(1, TimeUnit.DAYS);
}
}
@Benchmark
public long test() {
return yaProstoRyadomStoyal.incAndGet();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment