This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-pool.git


The following commit(s) were added to refs/heads/master by this push:
     new 6694bf8f Add EvictionConfig.isEvictionThread()
6694bf8f is described below

commit 6694bf8f262d98562b2a9b2ca032335510c24e56
Author: Gary Gregory <[email protected]>
AuthorDate: Sat Jan 17 07:38:31 2026 -0500

    Add EvictionConfig.isEvictionThread()
    
    - Use simpler Thread constructor.
    - Handy for advanced use and testing.
---
 .../apache/commons/pool3/impl/EvictionConfig.java  | 16 ++++++++++++++
 .../apache/commons/pool3/impl/EvictionTimer.java   | 25 ++++++++++------------
 .../commons/pool3/impl/TestGenericObjectPool.java  |  5 +++--
 3 files changed, 30 insertions(+), 16 deletions(-)

diff --git a/src/main/java/org/apache/commons/pool3/impl/EvictionConfig.java 
b/src/main/java/org/apache/commons/pool3/impl/EvictionConfig.java
index 15c9e4c1..00e22b18 100644
--- a/src/main/java/org/apache/commons/pool3/impl/EvictionConfig.java
+++ b/src/main/java/org/apache/commons/pool3/impl/EvictionConfig.java
@@ -31,6 +31,22 @@ 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/pool3/impl/EvictionTimer.java 
b/src/main/java/org/apache/commons/pool3/impl/EvictionTimer.java
index 42746428..3e5bef80 100644
--- a/src/main/java/org/apache/commons/pool3/impl/EvictionTimer.java
+++ b/src/main/java/org/apache/commons/pool3/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     Delay in milliseconds before task is executed.
      * @param period    Time in milliseconds 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/pool3/impl/TestGenericObjectPool.java 
b/src/test/java/org/apache/commons/pool3/impl/TestGenericObjectPool.java
index e4bc90df..a066da9c 100644
--- a/src/test/java/org/apache/commons/pool3/impl/TestGenericObjectPool.java
+++ b/src/test/java/org/apache/commons/pool3/impl/TestGenericObjectPool.java
@@ -457,8 +457,8 @@ class TestGenericObjectPool extends TestBaseObjectPool {
         private final AtomicInteger callCount = new AtomicInteger();
 
         @Override
-        public boolean evict(final EvictionConfig config, final 
PooledObject<T> underTest,
-                final int idleCount) {
+        public boolean evict(final EvictionConfig config, final 
PooledObject<T> underTest, final int idleCount) {
+            assertTrue(EvictionConfig.isEvictionThread());
             return callCount.incrementAndGet() > 1500;
         }
     }
@@ -1661,6 +1661,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);

Reply via email to