Skip to content

Commit

Permalink
Fix thread local fallback initialization (#515)
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-manes committed Mar 17, 2021
1 parent 50af7d7 commit cbc71f2
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ static final class VarHandleProbe implements Probe {

/** Uses a thread local to maintain a random probe value. */
static final class ThreadLocalProbe implements Probe {
static final ThreadLocal<int[]> threadHashCode = new ThreadLocal<>();
static final ThreadLocal<int[]> threadHashCode = ThreadLocal.withInitial(() -> new int[1]);

@Override public int get() {
return threadHashCode.get()[0];
Expand All @@ -351,7 +351,7 @@ static final class ThreadLocalProbe implements Probe {
@Override public void initialize() {
// Avoid zero to allow xorShift rehash
int hash = 1 | ThreadLocalRandom.current().nextInt();
threadHashCode.set(new int[] { hash });
threadHashCode.get()[0] = hash;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public void init(FakeBuffer<Integer> buffer) {

@Test(dataProvider = "probes")
public void probe(Probe probe) {
probe.get();
probe.initialize();
assertThat(probe.get(), is(not(0)));

Expand Down

0 comments on commit cbc71f2

Please sign in to comment.