Skip to content

Java virtual threads locked up / parking / permanently blocking -- swap synchronized block for reentrant lock #339

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: POOL_2_X
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions src/main/java/org/apache/commons/pool2/impl/GenericObjectPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors;

import org.apache.commons.pool2.DestroyMode;
Expand Down Expand Up @@ -118,7 +120,8 @@ private static void wait(final Object obj, final Duration duration) throws Inter

private long makeObjectCount;

private final Object makeObjectCountLock = new Object();
private final ReentrantLock makeObjectCountLock = new ReentrantLock();
private final Condition makeObjectCountLockCondition = makeObjectCountLock.newCondition();

private final LinkedBlockingDeque<PooledObject<T>> idleObjects;

Expand Down Expand Up @@ -524,7 +527,8 @@ private PooledObject<T> create() throws Exception {
// call the factory
Boolean create = null;
while (create == null) {
synchronized (makeObjectCountLock) {
makeObjectCountLock.lock();
try {
final long newCreateCount = createCount.incrementAndGet();
if (newCreateCount > localMaxTotal) {
// The pool is currently at capacity or in the process of
Expand All @@ -540,13 +544,15 @@ private PooledObject<T> create() throws Exception {
// bring the pool to capacity. Those calls might also
// fail so wait until they complete and then re-test if
// the pool is at capacity or not.
wait(makeObjectCountLock, localMaxWaitDuration);
makeObjectCountLockCondition.awaitNanos(localMaxWaitDuration.toNanos());
}
} else {
// The pool is not at capacity. Create a new object.
makeObjectCount++;
create = Boolean.TRUE;
}
} finally {
makeObjectCountLock.unlock();
}

// Do not block more if maxWaitTimeMillis is set.
Expand Down Expand Up @@ -575,9 +581,12 @@ private PooledObject<T> create() throws Exception {
createCount.decrementAndGet();
throw e;
} finally {
synchronized (makeObjectCountLock) {
makeObjectCountLock.lock();
try {
makeObjectCount--;
makeObjectCountLock.notifyAll();
makeObjectCountLockCondition.signalAll();
} finally {
makeObjectCountLock.unlock();
}
}

Expand Down