From 009a7013cf75b69290abb9bd24b12b66b55a07bf Mon Sep 17 00:00:00 2001 From: Brett Wooldridge Date: Sat, 25 Apr 2020 21:22:21 +0900 Subject: [PATCH] Qualify yield() method as Thread.yield() (IDE warning for Java 14+), and add threadlocal removal attempt to bag item removal. --- .../com/zaxxer/hikari/util/ConcurrentBag.java | 33 +++++++++---------- 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/zaxxer/hikari/util/ConcurrentBag.java b/src/main/java/com/zaxxer/hikari/util/ConcurrentBag.java index c6a9b2f0..9553c204 100644 --- a/src/main/java/com/zaxxer/hikari/util/ConcurrentBag.java +++ b/src/main/java/com/zaxxer/hikari/util/ConcurrentBag.java @@ -15,17 +15,9 @@ */ package com.zaxxer.hikari.util; -import static java.lang.Thread.yield; -import static java.util.concurrent.TimeUnit.MICROSECONDS; -import static java.util.concurrent.TimeUnit.NANOSECONDS; -import static java.util.concurrent.locks.LockSupport.parkNanos; - -import static com.zaxxer.hikari.util.ClockSource.currentTime; -import static com.zaxxer.hikari.util.ClockSource.elapsedNanos; -import static com.zaxxer.hikari.util.ConcurrentBag.IConcurrentBagEntry.STATE_IN_USE; -import static com.zaxxer.hikari.util.ConcurrentBag.IConcurrentBagEntry.STATE_NOT_IN_USE; -import static com.zaxxer.hikari.util.ConcurrentBag.IConcurrentBagEntry.STATE_REMOVED; -import static com.zaxxer.hikari.util.ConcurrentBag.IConcurrentBagEntry.STATE_RESERVED; +import com.zaxxer.hikari.util.ConcurrentBag.IConcurrentBagEntry; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.lang.ref.WeakReference; import java.util.ArrayList; @@ -37,10 +29,12 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.zaxxer.hikari.util.ConcurrentBag.IConcurrentBagEntry; +import static com.zaxxer.hikari.util.ClockSource.currentTime; +import static com.zaxxer.hikari.util.ClockSource.elapsedNanos; +import static com.zaxxer.hikari.util.ConcurrentBag.IConcurrentBagEntry.*; +import static java.util.concurrent.TimeUnit.MICROSECONDS; +import static java.util.concurrent.TimeUnit.NANOSECONDS; +import static java.util.concurrent.locks.LockSupport.parkNanos; /** * This is a specialized concurrent bag that achieves superior performance @@ -190,7 +184,7 @@ public class ConcurrentBag implements AutoCloseab parkNanos(MICROSECONDS.toNanos(10)); } else { - yield(); + Thread.yield(); } } @@ -216,7 +210,7 @@ public class ConcurrentBag implements AutoCloseab // spin until a thread takes it or none are waiting while (waiters.get() > 0 && bagEntry.getState() == STATE_NOT_IN_USE && !handoffQueue.offer(bagEntry)) { - yield(); + Thread.yield(); } } @@ -241,6 +235,8 @@ public class ConcurrentBag implements AutoCloseab LOGGER.warn("Attempt to remove an object from the bag that does not exist: {}", bagEntry); } + threadList.get().remove(bagEntry); + return removed; } @@ -306,12 +302,13 @@ public class ConcurrentBag implements AutoCloseab * * @param bagEntry the item to unreserve */ + @SuppressWarnings("SpellCheckingInspection") 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)) { - yield(); + Thread.yield(); } } else {