Skip to content

Instantly share code, notes, and snippets.

@srvaroa
Last active October 15, 2020 11:09
Show Gist options
  • Select an option

  • Save srvaroa/aa1f6c273ea6ae215bbf43e481f7689e to your computer and use it in GitHub Desktop.

Select an option

Save srvaroa/aa1f6c273ea6ae215bbf43e481f7689e to your computer and use it in GitHub Desktop.
package com.github.srvaroa.jmh;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;
import java.util.concurrent.TimeUnit;
@State(Scope.Benchmark)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Warmup(iterations = 4)
@Fork(value = 5, jvmArgsAppend = {"-XX:-UseBiasedLocking", "-XX:BiasedLockingStartupDelay=0"})
public class BiasedLockingBenchmark {
int unsafeCounter = 0;
Object withIdHash;
Object withoutIdHash;
@Setup
public void setup() {
withIdHash = new Object();
withoutIdHash = new Object() {
@Override
public int hashCode() {
return 1;
}
};
withIdHash.hashCode();
withoutIdHash.hashCode();
}
@Benchmark
public void withIdHash(Blackhole bh) {
synchronized(withIdHash) {
bh.consume(unsafeCounter++);
}
}
@Benchmark
public void withoutIdHash(Blackhole bh) {
synchronized(withoutIdHash) {
bh.consume(unsafeCounter++);
}
}
@Benchmark
@Threads(2)
public void withoutIdHashContended(Blackhole bh) {
synchronized(withoutIdHash) {
bh.consume(unsafeCounter++);
}
}
@Benchmark
@Threads(2)
public void withIdHashContended(Blackhole bh) {
synchronized(withIdHash) {
bh.consume(unsafeCounter++);
}
}
}
@yamanyar

Copy link
Copy Markdown

withIdHashContended and withoutIdHashContended are exeactly same

@srvaroa

srvaroa commented Oct 15, 2020

Copy link
Copy Markdown
Author

Thanks! fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment