Skip to content

Commit 9fa66d1

Browse files
minor code cleanup
1 parent 1164493 commit 9fa66d1

File tree

1 file changed

+16
-19
lines changed

1 file changed

+16
-19
lines changed

src/main/java/com/zaxxer/hikari/util/ConcurrentBag.java

+16-19
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ public class ConcurrentBag<T extends IConcurrentBagEntry> implements AutoCloseab
6161
private static final Logger LOGGER = LoggerFactory.getLogger(ConcurrentBag.class);
6262

6363
private final CopyOnWriteArrayList<T> sharedList;
64-
private final boolean weakThreadLocals;
64+
private final boolean useWeakThreadLocals;
6565

66-
private final ThreadLocal<List<Object>> threadList;
66+
private final ThreadLocal<List<Object>> threadLocalList;
6767
private final IBagStateListener listener;
6868
private final AtomicInteger waiters;
6969
private volatile boolean closed;
@@ -95,17 +95,14 @@ public interface IBagStateListener
9595
public ConcurrentBag(final IBagStateListener listener)
9696
{
9797
this.listener = listener;
98-
this.weakThreadLocals = useWeakThreadLocals();
98+
this.useWeakThreadLocals = useWeakThreadLocals();
9999

100100
this.handoffQueue = new SynchronousQueue<>(true);
101101
this.waiters = new AtomicInteger();
102102
this.sharedList = new CopyOnWriteArrayList<>();
103-
if (weakThreadLocals) {
104-
this.threadList = ThreadLocal.withInitial(() -> new ArrayList<>(16));
105-
}
106-
else {
107-
this.threadList = ThreadLocal.withInitial(() -> new FastList<>(IConcurrentBagEntry.class, 16));
108-
}
103+
this.threadLocalList = ThreadLocal.withInitial(() ->
104+
useWeakThreadLocals ? new ArrayList<>(16) : new FastList<>(IConcurrentBagEntry.class, 16)
105+
);
109106
}
110107

111108
/**
@@ -120,18 +117,18 @@ public ConcurrentBag(final IBagStateListener listener)
120117
public T borrow(long timeout, final TimeUnit timeUnit) throws InterruptedException
121118
{
122119
// Try the thread-local list first
123-
final var list = threadList.get();
124-
for (int i = list.size() - 1; i >= 0; i--) {
120+
final var list = threadLocalList.get();
121+
for (var i = list.size() - 1; i >= 0; i--) {
125122
final var entry = list.remove(i);
126123
@SuppressWarnings("unchecked")
127-
final T bagEntry = weakThreadLocals ? ((WeakReference<T>) entry).get() : (T) entry;
124+
final T bagEntry = useWeakThreadLocals ? ((WeakReference<T>) entry).get() : (T) entry;
128125
if (bagEntry != null && bagEntry.compareAndSet(STATE_NOT_IN_USE, STATE_IN_USE)) {
129126
return bagEntry;
130127
}
131128
}
132129

133130
// Otherwise, scan the shared list ... then poll the handoff queue
134-
final int waiting = waiters.incrementAndGet();
131+
final var waiting = waiters.incrementAndGet();
135132
try {
136133
for (T bagEntry : sharedList) {
137134
if (bagEntry.compareAndSet(STATE_NOT_IN_USE, STATE_IN_USE)) {
@@ -188,9 +185,9 @@ else if ((i & 0xff) == 0xff) {
188185
}
189186
}
190187

191-
final var threadLocalList = threadList.get();
192-
if (threadLocalList.size() < 50) {
193-
threadLocalList.add(weakThreadLocals ? new WeakReference<>(bagEntry) : bagEntry);
188+
final var threadLocalEntries = this.threadLocalList.get();
189+
if (threadLocalEntries.size() < 16) {
190+
threadLocalEntries.add(useWeakThreadLocals ? new WeakReference<>(bagEntry) : bagEntry);
194191
}
195192
}
196193

@@ -230,12 +227,12 @@ public boolean remove(final T bagEntry)
230227
return false;
231228
}
232229

233-
final boolean removed = sharedList.remove(bagEntry);
230+
final var removed = sharedList.remove(bagEntry);
234231
if (!removed && !closed) {
235232
LOGGER.warn("Attempt to remove an object from the bag that does not exist: {}", bagEntry);
236233
}
237234

238-
threadList.get().remove(bagEntry);
235+
threadLocalList.get().remove(bagEntry);
239236

240237
return removed;
241238
}
@@ -307,7 +304,7 @@ public void unreserve(final T bagEntry)
307304
{
308305
if (bagEntry.compareAndSet(STATE_RESERVED, STATE_NOT_IN_USE)) {
309306
// spin until a thread takes it or none are waiting
310-
while (waiters.get() > 0 && !handoffQueue.offer(bagEntry)) {
307+
while (waiters.get() > 0 && bagEntry.getState() == STATE_NOT_IN_USE && !handoffQueue.offer(bagEntry)) {
311308
Thread.yield();
312309
}
313310
}

0 commit comments

Comments
 (0)