This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch POOL_2_X
in repository https://gitbox.apache.org/repos/asf/commons-pool.git
The following commit(s) were added to refs/heads/POOL_2_X by this push:
new 9b6a3404 Add EvictionConfig.isEvictionThread()
9b6a3404 is described below
commit 9b6a340482a4e21ac03bbb6789c2ade38e090b99
Author: Gary Gregory <[email protected]>
AuthorDate: Sat Jan 17 07:23:12 2026 -0500
Add EvictionConfig.isEvictionThread()
- Use simpler Thread constructor.
- Handy for advanced use and testing.
---
src/changes/changes.xml | 1 +
.../apache/commons/pool2/impl/EvictionConfig.java | 17 +++++++++++++++
.../apache/commons/pool2/impl/EvictionTimer.java | 25 ++++++++++------------
.../commons/pool2/impl/TestGenericObjectPool.java | 2 ++
4 files changed, 31 insertions(+), 14 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index c2391502..efae18a5 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -50,6 +50,7 @@ The <action> type attribute can be add,update,fix,remove.
<!-- FIX -->
<!-- ADD -->
<action type="add" dev="ggregory" due-to="Philip Helger, Gary Gregory"
issue="POOL-430">Make AbandonedConfig.DEFAULT_REMOVE_ABANDONED_TIMEOUT_DURATION
public.</action>
+ <action type="add" dev="ggregory" due-to="Gary Gregory">Add
EvictionConfig.isEvictionThread().</action>
<!-- UPDATE -->
<action type="update" dev="ggregory" due-to="Gary Gregory">Bump
org.apache.commons:commons-parent from 93 to 95.</action>
</release>
diff --git a/src/main/java/org/apache/commons/pool2/impl/EvictionConfig.java
b/src/main/java/org/apache/commons/pool2/impl/EvictionConfig.java
index 27feca91..85bba68a 100644
--- a/src/main/java/org/apache/commons/pool2/impl/EvictionConfig.java
+++ b/src/main/java/org/apache/commons/pool2/impl/EvictionConfig.java
@@ -31,8 +31,25 @@ import java.time.Duration;
public class EvictionConfig {
private static final Duration MAX_DURATION =
Duration.ofMillis(Long.MAX_VALUE);
+
+ /**
+ * The eviction thread name.
+ */
+ static final String THREAD_NAME = "commons-pool-evictor";
+
+ /**
+ * Tests whether the current thread is the eviction thread.
+ *
+ * @return whether the current thread is the eviction thread.
+ * @since 2.14.0
+ */
+ public static boolean isEvictionThread() {
+ return Thread.currentThread().getName().equals(THREAD_NAME);
+ }
+
private final Duration idleEvictDuration;
private final Duration idleSoftEvictDuration;
+
private final int minIdle;
/**
diff --git a/src/main/java/org/apache/commons/pool2/impl/EvictionTimer.java
b/src/main/java/org/apache/commons/pool2/impl/EvictionTimer.java
index d4deb946..cf51cd0d 100644
--- a/src/main/java/org/apache/commons/pool2/impl/EvictionTimer.java
+++ b/src/main/java/org/apache/commons/pool2/impl/EvictionTimer.java
@@ -55,13 +55,12 @@ final class EvictionTimer {
@Override
public Thread newThread(final Runnable runnable) {
- final Thread thread = new Thread(null, runnable,
"commons-pool-evictor");
+ final Thread thread = new Thread(runnable,
EvictionConfig.THREAD_NAME);
thread.setDaemon(true); // POOL-363 - Required for applications
using Runtime.addShutdownHook().
AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
thread.setContextClassLoader(EvictorThreadFactory.class.getClassLoader());
return null;
});
-
return thread;
}
}
@@ -132,7 +131,7 @@ final class EvictionTimer {
/** Executor instance */
private static ScheduledThreadPoolExecutor executor;
//@GuardedBy("EvictionTimer.class")
- /** Keys are weak references to tasks, values are runners managed by
executor. */
+ /** Keys are weak references to pools, values are runners managed by
executor. */
private static final HashMap<
WeakReference<BaseGenericObjectPool<?>.Evictor>,
WeakRunner<BaseGenericObjectPool<?>.Evictor>> TASK_MAP = new
HashMap<>(); // @GuardedBy("EvictionTimer.class")
@@ -175,6 +174,8 @@ final class EvictionTimer {
}
/**
+ * Gets the number of eviction tasks under management.
+ *
* @return the number of eviction tasks under management.
*/
static synchronized int getNumTasks() {
@@ -191,7 +192,7 @@ final class EvictionTimer {
}
/**
- * Removes evictor from the task set and executor.
+ * Removes an evictor from the task set and executor.
* Only called when holding the class lock.
*
* @param evictor Eviction task to remove
@@ -213,22 +214,20 @@ final class EvictionTimer {
* to cancel the task to prevent memory and/or thread leaks in application
* server environments.
*
- * @param task Task to be scheduled.
+ * @param pool Task to be scheduled.
* @param delay Duration before task is executed.
* @param period Duration between executions.
*/
- static synchronized void schedule(
- final BaseGenericObjectPool<?>.Evictor task, final Duration delay,
final Duration period) {
+ static synchronized void schedule(final BaseGenericObjectPool<?>.Evictor
pool, final Duration delay, final Duration period) {
if (null == executor) {
executor = new ScheduledThreadPoolExecutor(1, new
EvictorThreadFactory());
executor.setRemoveOnCancelPolicy(true);
executor.scheduleAtFixedRate(new Reaper(), delay.toMillis(),
period.toMillis(), TimeUnit.MILLISECONDS);
}
- final WeakReference<BaseGenericObjectPool<?>.Evictor> ref = new
WeakReference<>(task);
+ final WeakReference<BaseGenericObjectPool<?>.Evictor> ref = new
WeakReference<>(pool);
final WeakRunner<BaseGenericObjectPool<?>.Evictor> runner = new
WeakRunner<>(ref);
- final ScheduledFuture<?> scheduledFuture =
executor.scheduleWithFixedDelay(runner, delay.toMillis(),
- period.toMillis(), TimeUnit.MILLISECONDS);
- task.setScheduledFuture(scheduledFuture);
+ final ScheduledFuture<?> scheduledFuture =
executor.scheduleWithFixedDelay(runner, delay.toMillis(), period.toMillis(),
TimeUnit.MILLISECONDS);
+ pool.setScheduledFuture(scheduledFuture);
TASK_MAP.put(ref, runner);
}
@@ -242,9 +241,7 @@ final class EvictionTimer {
*/
@Override
public String toString() {
- final StringBuilder builder = new StringBuilder();
- builder.append("EvictionTimer []");
- return builder.toString();
+ return "EvictionTimer []";
}
}
diff --git
a/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java
b/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java
index 4e7f90e8..1e22de82 100644
--- a/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java
+++ b/src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java
@@ -440,6 +440,7 @@ class TestGenericObjectPool extends TestBaseObjectPool {
@Override
public boolean evict(final EvictionConfig config, final
PooledObject<T> underTest, final int idleCount) {
+ assertTrue(EvictionConfig.isEvictionThread());
return callCount.incrementAndGet() > 1500;
}
}
@@ -1692,6 +1693,7 @@ class TestGenericObjectPool extends TestBaseObjectPool {
@Test
@Timeout(value = 60000, unit = TimeUnit.MILLISECONDS)
void testEvictionPolicy() throws Exception {
+ assertFalse(EvictionConfig.isEvictionThread());
genericObjectPool.setMaxIdle(500);
genericObjectPool.setMaxTotal(500);
genericObjectPool.setNumTestsPerEvictionRun(500);