From cbc71f24a1732106308ff69ff619195374680b56 Mon Sep 17 00:00:00 2001 From: Ben Manes Date: Wed, 17 Mar 2021 11:11:56 -0700 Subject: [PATCH] Fix thread local fallback initialization (#515) --- .../com/github/benmanes/caffeine/cache/StripedBuffer.java | 4 ++-- .../com/github/benmanes/caffeine/cache/StripedBufferTest.java | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/StripedBuffer.java b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/StripedBuffer.java index 78df760734..a35bfd8862 100644 --- a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/StripedBuffer.java +++ b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/StripedBuffer.java @@ -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 threadHashCode = new ThreadLocal<>(); + static final ThreadLocal threadHashCode = ThreadLocal.withInitial(() -> new int[1]); @Override public int get() { return threadHashCode.get()[0]; @@ -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; } } } diff --git a/caffeine/src/test/java/com/github/benmanes/caffeine/cache/StripedBufferTest.java b/caffeine/src/test/java/com/github/benmanes/caffeine/cache/StripedBufferTest.java index e74b379088..c11ee511b7 100644 --- a/caffeine/src/test/java/com/github/benmanes/caffeine/cache/StripedBufferTest.java +++ b/caffeine/src/test/java/com/github/benmanes/caffeine/cache/StripedBufferTest.java @@ -49,6 +49,7 @@ public void init(FakeBuffer buffer) { @Test(dataProvider = "probes") public void probe(Probe probe) { + probe.get(); probe.initialize(); assertThat(probe.get(), is(not(0)));