Skip to content

Commit 9b6a340

Browse files
committed
Add EvictionConfig.isEvictionThread()
- Use simpler Thread constructor. - Handy for advanced use and testing.
1 parent 069a1be commit 9b6a340

File tree

4 files changed

+31
-14
lines changed

4 files changed

+31
-14
lines changed

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ The <action> type attribute can be add,update,fix,remove.
5050
<!-- FIX -->
5151
<!-- ADD -->
5252
<action type="add" dev="ggregory" due-to="Philip Helger, Gary Gregory" issue="POOL-430">Make AbandonedConfig.DEFAULT_REMOVE_ABANDONED_TIMEOUT_DURATION public.</action>
53+
<action type="add" dev="ggregory" due-to="Gary Gregory">Add EvictionConfig.isEvictionThread().</action>
5354
<!-- UPDATE -->
5455
<action type="update" dev="ggregory" due-to="Gary Gregory">Bump org.apache.commons:commons-parent from 93 to 95.</action>
5556
</release>

src/main/java/org/apache/commons/pool2/impl/EvictionConfig.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,25 @@
3131
public class EvictionConfig {
3232

3333
private static final Duration MAX_DURATION = Duration.ofMillis(Long.MAX_VALUE);
34+
35+
/**
36+
* The eviction thread name.
37+
*/
38+
static final String THREAD_NAME = "commons-pool-evictor";
39+
40+
/**
41+
* Tests whether the current thread is the eviction thread.
42+
*
43+
* @return whether the current thread is the eviction thread.
44+
* @since 2.14.0
45+
*/
46+
public static boolean isEvictionThread() {
47+
return Thread.currentThread().getName().equals(THREAD_NAME);
48+
}
49+
3450
private final Duration idleEvictDuration;
3551
private final Duration idleSoftEvictDuration;
52+
3653
private final int minIdle;
3754

3855
/**

src/main/java/org/apache/commons/pool2/impl/EvictionTimer.java

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,12 @@ private static final class EvictorThreadFactory implements ThreadFactory {
5555

5656
@Override
5757
public Thread newThread(final Runnable runnable) {
58-
final Thread thread = new Thread(null, runnable, "commons-pool-evictor");
58+
final Thread thread = new Thread(runnable, EvictionConfig.THREAD_NAME);
5959
thread.setDaemon(true); // POOL-363 - Required for applications using Runtime.addShutdownHook().
6060
AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
6161
thread.setContextClassLoader(EvictorThreadFactory.class.getClassLoader());
6262
return null;
6363
});
64-
6564
return thread;
6665
}
6766
}
@@ -132,7 +131,7 @@ public void run() {
132131
/** Executor instance */
133132
private static ScheduledThreadPoolExecutor executor; //@GuardedBy("EvictionTimer.class")
134133

135-
/** Keys are weak references to tasks, values are runners managed by executor. */
134+
/** Keys are weak references to pools, values are runners managed by executor. */
136135
private static final HashMap<
137136
WeakReference<BaseGenericObjectPool<?>.Evictor>,
138137
WeakRunner<BaseGenericObjectPool<?>.Evictor>> TASK_MAP = new HashMap<>(); // @GuardedBy("EvictionTimer.class")
@@ -175,6 +174,8 @@ static ScheduledThreadPoolExecutor getExecutor() {
175174
}
176175

177176
/**
177+
* Gets the number of eviction tasks under management.
178+
*
178179
* @return the number of eviction tasks under management.
179180
*/
180181
static synchronized int getNumTasks() {
@@ -191,7 +192,7 @@ static HashMap<WeakReference<BaseGenericObjectPool<?>.Evictor>, WeakRunner<BaseG
191192
}
192193

193194
/**
194-
* Removes evictor from the task set and executor.
195+
* Removes an evictor from the task set and executor.
195196
* Only called when holding the class lock.
196197
*
197198
* @param evictor Eviction task to remove
@@ -213,22 +214,20 @@ private static void remove(final BaseGenericObjectPool<?>.Evictor evictor) {
213214
* to cancel the task to prevent memory and/or thread leaks in application
214215
* server environments.
215216
*
216-
* @param task Task to be scheduled.
217+
* @param pool Task to be scheduled.
217218
* @param delay Duration before task is executed.
218219
* @param period Duration between executions.
219220
*/
220-
static synchronized void schedule(
221-
final BaseGenericObjectPool<?>.Evictor task, final Duration delay, final Duration period) {
221+
static synchronized void schedule(final BaseGenericObjectPool<?>.Evictor pool, final Duration delay, final Duration period) {
222222
if (null == executor) {
223223
executor = new ScheduledThreadPoolExecutor(1, new EvictorThreadFactory());
224224
executor.setRemoveOnCancelPolicy(true);
225225
executor.scheduleAtFixedRate(new Reaper(), delay.toMillis(), period.toMillis(), TimeUnit.MILLISECONDS);
226226
}
227-
final WeakReference<BaseGenericObjectPool<?>.Evictor> ref = new WeakReference<>(task);
227+
final WeakReference<BaseGenericObjectPool<?>.Evictor> ref = new WeakReference<>(pool);
228228
final WeakRunner<BaseGenericObjectPool<?>.Evictor> runner = new WeakRunner<>(ref);
229-
final ScheduledFuture<?> scheduledFuture = executor.scheduleWithFixedDelay(runner, delay.toMillis(),
230-
period.toMillis(), TimeUnit.MILLISECONDS);
231-
task.setScheduledFuture(scheduledFuture);
229+
final ScheduledFuture<?> scheduledFuture = executor.scheduleWithFixedDelay(runner, delay.toMillis(), period.toMillis(), TimeUnit.MILLISECONDS);
230+
pool.setScheduledFuture(scheduledFuture);
232231
TASK_MAP.put(ref, runner);
233232
}
234233

@@ -242,9 +241,7 @@ private EvictionTimer() {
242241
*/
243242
@Override
244243
public String toString() {
245-
final StringBuilder builder = new StringBuilder();
246-
builder.append("EvictionTimer []");
247-
return builder.toString();
244+
return "EvictionTimer []";
248245
}
249246

250247
}

src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,7 @@ public static class TestEvictionPolicy<T> implements EvictionPolicy<T> {
440440

441441
@Override
442442
public boolean evict(final EvictionConfig config, final PooledObject<T> underTest, final int idleCount) {
443+
assertTrue(EvictionConfig.isEvictionThread());
443444
return callCount.incrementAndGet() > 1500;
444445
}
445446
}
@@ -1692,6 +1693,7 @@ void testEvictionOrder() throws Exception {
16921693
@Test
16931694
@Timeout(value = 60000, unit = TimeUnit.MILLISECONDS)
16941695
void testEvictionPolicy() throws Exception {
1696+
assertFalse(EvictionConfig.isEvictionThread());
16951697
genericObjectPool.setMaxIdle(500);
16961698
genericObjectPool.setMaxTotal(500);
16971699
genericObjectPool.setNumTestsPerEvictionRun(500);

0 commit comments

Comments
 (0)