Package org.apache.commons.pool2.impl
Class EvictionTimer
- java.lang.Object
-
- org.apache.commons.pool2.impl.EvictionTimer
-
final class EvictionTimer extends java.lang.ObjectProvides a shared idle object eviction timer for all pools.This class is currently implemented using
ScheduledThreadPoolExecutor. This implementation may change in any future release. This class keeps track of how many pools are using it. If no pools are using the timer, it is canceled. This prevents a thread being left running which, in application server environments, can lead to memory leads and/or prevent applications from shutting down or reloading cleanly.This class has package scope to prevent its inclusion in the pool public API. The class declaration below should *not* be changed to public.
This class is intended to be thread-safe.
- Since:
- 2.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classEvictionTimer.EvictorThreadFactoryThread factory that creates a daemon thread, with the context class loader from this class.private static classEvictionTimer.ReaperTask that removes references to abandoned tasks and shuts down the executor if there are no live tasks left.private static classEvictionTimer.WeakRunner<R extends java.lang.Runnable>Runnable that runs the referent of a weak reference.
-
Field Summary
Fields Modifier and Type Field Description private static java.util.concurrent.ScheduledThreadPoolExecutorexecutorExecutor instanceprivate static java.util.HashMap<java.lang.ref.WeakReference<BaseGenericObjectPool.Evictor>,EvictionTimer.WeakRunner<BaseGenericObjectPool.Evictor>>TASK_MAPKeys are weak references to tasks, values are runners managed by executor.
-
Constructor Summary
Constructors Modifier Constructor Description privateEvictionTimer()Prevents instantiation
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) static voidcancel(BaseGenericObjectPool.Evictor evictor, java.time.Duration timeout, boolean restarting)Removes the specified eviction task from the timer.(package private) static java.util.concurrent.ScheduledThreadPoolExecutorgetExecutor()For testing only.(package private) static intgetNumTasks()(package private) static java.util.HashMap<java.lang.ref.WeakReference<BaseGenericObjectPool.Evictor>,EvictionTimer.WeakRunner<BaseGenericObjectPool.Evictor>>getTaskMap()Gets the task map.private static voidremove(BaseGenericObjectPool.Evictor evictor)Removes evictor from the task set and executor.(package private) static voidschedule(BaseGenericObjectPool.Evictor task, java.time.Duration delay, java.time.Duration period)Adds the specified eviction task to the timer.java.lang.StringtoString()
-
-
-
Field Detail
-
executor
private static java.util.concurrent.ScheduledThreadPoolExecutor executor
Executor instance
-
TASK_MAP
private static final java.util.HashMap<java.lang.ref.WeakReference<BaseGenericObjectPool.Evictor>,EvictionTimer.WeakRunner<BaseGenericObjectPool.Evictor>> TASK_MAP
Keys are weak references to tasks, values are runners managed by executor.
-
-
Method Detail
-
cancel
static void cancel(BaseGenericObjectPool.Evictor evictor, java.time.Duration timeout, boolean restarting)
Removes the specified eviction task from the timer.- Parameters:
evictor- Task to be canceled.timeout- If the associated executor is no longer required, how long should this thread wait for the executor to terminate?restarting- The state of the evictor.
-
getExecutor
static java.util.concurrent.ScheduledThreadPoolExecutor getExecutor()
For testing only.- Returns:
- The executor.
-
getNumTasks
static int getNumTasks()
- Returns:
- the number of eviction tasks under management.
-
getTaskMap
static java.util.HashMap<java.lang.ref.WeakReference<BaseGenericObjectPool.Evictor>,EvictionTimer.WeakRunner<BaseGenericObjectPool.Evictor>> getTaskMap()
Gets the task map. Keys are weak references to tasks, values are runners managed by executor.- Returns:
- the task map.
-
remove
private static void remove(BaseGenericObjectPool.Evictor evictor)
Removes evictor from the task set and executor. Only called when holding the class lock.- Parameters:
evictor- Eviction task to remove
-
schedule
static void schedule(BaseGenericObjectPool.Evictor task, java.time.Duration delay, java.time.Duration period)
Adds the specified eviction task to the timer. Tasks that are added with a call to this method *must* callcancel(BaseGenericObjectPool.Evictor, Duration, boolean)to cancel the task to prevent memory and/or thread leaks in application server environments.- Parameters:
task- Task to be scheduled.delay- Duration before task is executed.period- Duration between executions.
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object- Since:
- 2.4.3
-
-