Skip to content

Commit cbc71f2

Browse files
committed
Fix thread local fallback initialization (#515)
1 parent 50af7d7 commit cbc71f2

File tree

2 files changed

+3
-2
lines changed

2 files changed

+3
-2
lines changed

caffeine/src/main/java/com/github/benmanes/caffeine/cache/StripedBuffer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ static final class VarHandleProbe implements Probe {
340340

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

345345
@Override public int get() {
346346
return threadHashCode.get()[0];
@@ -351,7 +351,7 @@ static final class ThreadLocalProbe implements Probe {
351351
@Override public void initialize() {
352352
// Avoid zero to allow xorShift rehash
353353
int hash = 1 | ThreadLocalRandom.current().nextInt();
354-
threadHashCode.set(new int[] { hash });
354+
threadHashCode.get()[0] = hash;
355355
}
356356
}
357357
}

caffeine/src/test/java/com/github/benmanes/caffeine/cache/StripedBufferTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public void init(FakeBuffer<Integer> buffer) {
4949

5050
@Test(dataProvider = "probes")
5151
public void probe(Probe probe) {
52+
probe.get();
5253
probe.initialize();
5354
assertThat(probe.get(), is(not(0)));
5455

0 commit comments

Comments
 (0)