From 9fa66d1f54121f8ef5061c51094a6a2daa2cf8f3 Mon Sep 17 00:00:00 2001 From: Brett Wooldridge Date: Sat, 30 Nov 2024 03:00:35 +0900 Subject: [PATCH] minor code cleanup --- .../com/zaxxer/hikari/util/ConcurrentBag.java | 35 +++++++++---------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/zaxxer/hikari/util/ConcurrentBag.java b/src/main/java/com/zaxxer/hikari/util/ConcurrentBag.java index 971cb8d75..6e2cd8e83 100644 --- a/src/main/java/com/zaxxer/hikari/util/ConcurrentBag.java +++ b/src/main/java/com/zaxxer/hikari/util/ConcurrentBag.java @@ -61,9 +61,9 @@ public class ConcurrentBag implements AutoCloseab private static final Logger LOGGER = LoggerFactory.getLogger(ConcurrentBag.class); private final CopyOnWriteArrayList sharedList; - private final boolean weakThreadLocals; + private final boolean useWeakThreadLocals; - private final ThreadLocal> threadList; + private final ThreadLocal> threadLocalList; private final IBagStateListener listener; private final AtomicInteger waiters; private volatile boolean closed; @@ -95,17 +95,14 @@ public interface IBagStateListener public ConcurrentBag(final IBagStateListener listener) { this.listener = listener; - this.weakThreadLocals = useWeakThreadLocals(); + this.useWeakThreadLocals = useWeakThreadLocals(); this.handoffQueue = new SynchronousQueue<>(true); this.waiters = new AtomicInteger(); this.sharedList = new CopyOnWriteArrayList<>(); - if (weakThreadLocals) { - this.threadList = ThreadLocal.withInitial(() -> new ArrayList<>(16)); - } - else { - this.threadList = ThreadLocal.withInitial(() -> new FastList<>(IConcurrentBagEntry.class, 16)); - } + this.threadLocalList = ThreadLocal.withInitial(() -> + useWeakThreadLocals ? new ArrayList<>(16) : new FastList<>(IConcurrentBagEntry.class, 16) + ); } /** @@ -120,18 +117,18 @@ public ConcurrentBag(final IBagStateListener listener) public T borrow(long timeout, final TimeUnit timeUnit) throws InterruptedException { // Try the thread-local list first - final var list = threadList.get(); - for (int i = list.size() - 1; i >= 0; i--) { + final var list = threadLocalList.get(); + for (var i = list.size() - 1; i >= 0; i--) { final var entry = list.remove(i); @SuppressWarnings("unchecked") - final T bagEntry = weakThreadLocals ? ((WeakReference) entry).get() : (T) entry; + final T bagEntry = useWeakThreadLocals ? ((WeakReference) entry).get() : (T) entry; if (bagEntry != null && bagEntry.compareAndSet(STATE_NOT_IN_USE, STATE_IN_USE)) { return bagEntry; } } // Otherwise, scan the shared list ... then poll the handoff queue - final int waiting = waiters.incrementAndGet(); + final var waiting = waiters.incrementAndGet(); try { for (T bagEntry : sharedList) { if (bagEntry.compareAndSet(STATE_NOT_IN_USE, STATE_IN_USE)) { @@ -188,9 +185,9 @@ else if ((i & 0xff) == 0xff) { } } - final var threadLocalList = threadList.get(); - if (threadLocalList.size() < 50) { - threadLocalList.add(weakThreadLocals ? new WeakReference<>(bagEntry) : bagEntry); + final var threadLocalEntries = this.threadLocalList.get(); + if (threadLocalEntries.size() < 16) { + threadLocalEntries.add(useWeakThreadLocals ? new WeakReference<>(bagEntry) : bagEntry); } } @@ -230,12 +227,12 @@ public boolean remove(final T bagEntry) return false; } - final boolean removed = sharedList.remove(bagEntry); + final var removed = sharedList.remove(bagEntry); if (!removed && !closed) { LOGGER.warn("Attempt to remove an object from the bag that does not exist: {}", bagEntry); } - threadList.get().remove(bagEntry); + threadLocalList.get().remove(bagEntry); return removed; } @@ -307,7 +304,7 @@ public void unreserve(final T bagEntry) { if (bagEntry.compareAndSet(STATE_RESERVED, STATE_NOT_IN_USE)) { // spin until a thread takes it or none are waiting - while (waiters.get() > 0 && !handoffQueue.offer(bagEntry)) { + while (waiters.get() > 0 && bagEntry.getState() == STATE_NOT_IN_USE && !handoffQueue.offer(bagEntry)) { Thread.yield(); } }