Package org.apache.commons.pool2.impl
Class EvictionTimer
java.lang.Object
org.apache.commons.pool2.impl.EvictionTimer
Provides 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
cancelled. 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 ClassesModifier and TypeClassDescriptionprivate static classThread factory that creates a daemon thread, with the context class loader from this class.private static classTask that removes references to abandoned tasks and shuts down the executor if there are no live tasks left.private static classEvictionTimer.WeakRunner<R extends Runnable>Runnable that runs the referent of a weak reference. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static ScheduledThreadPoolExecutorExecutor instanceprivate static final HashMap<WeakReference<BaseGenericObjectPool<?>.Evictor>,EvictionTimer.WeakRunner<BaseGenericObjectPool<?>.Evictor>> Keys are weak references to tasks, values are runners managed by executor. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription(package private) static voidcancel(BaseGenericObjectPool<?>.Evictor evictor, Duration timeout, boolean restarting) Removes the specified eviction task from the timer.(package private) static ScheduledThreadPoolExecutorFor testing only.(package private) static int(package private) static HashMap<WeakReference<BaseGenericObjectPool<?>.Evictor>,EvictionTimer.WeakRunner<BaseGenericObjectPool<?>.Evictor>> 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, Duration delay, Duration period) Adds the specified eviction task to the timer.toString()
-
Field Details
-
executor
Executor instance -
TASK_MAP
private static final HashMap<WeakReference<BaseGenericObjectPool<?>.Evictor>,EvictionTimer.WeakRunner<BaseGenericObjectPool<?>.Evictor>> TASK_MAPKeys are weak references to tasks, values are runners managed by executor.
-
-
Constructor Details
-
EvictionTimer
private EvictionTimer()Prevents instantiation
-
-
Method Details
-
cancel
Removes the specified eviction task from the timer.- Parameters:
evictor- Task to be cancelled.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
For testing only.- Returns:
- The executor.
-
getNumTasks
static int getNumTasks()- Returns:
- the number of eviction tasks under management.
-
getTaskMap
static HashMap<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
Removes evictor from the task set and executor. Only called when holding the class lock.- Parameters:
evictor- Eviction task to remove
-
schedule
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- Delay in milliseconds before task is executed.period- Time in milliseconds between executions.
-
toString
-