Top Description Inners Fields Constructors Methods
org.apache.commons.pool2.impl

pack-priv Class EvictionTimer

extends Object
Class Inheritance
Imports
java.lang.ref.WeakReference, java.security.AccessController, .PrivilegedAction, java.time.Duration, java.util.HashMap, .Map.Entry, java.util.concurrent.ScheduledFuture, .ScheduledThreadPoolExecutor, .ThreadFactory, .TimeUnit

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 and Inner Type Summary

Modifier and TypeClass and Description
private static class
EvictionTimer.EvictorThreadFactory

Thread factory that creates a daemon thread, with the context class loader from this class.

private static class
EvictionTimer.Reaper

Task that removes references to abandoned tasks and shuts down the executor if there are no live tasks left.

private static class
EvictionTimer.WeakRunner<
The kind of Runnable.
R extends Runnable
>

Runnable that runs the referent of a weak reference.

Field Summary

Modifier and TypeField and Description
private static ScheduledThreadPoolExecutor
executor

Executor instance

private static final HashMap<WeakReference<BaseGenericObjectPool<?>.Evictor>, EvictionTimer.WeakRunner<BaseGenericObjectPool<?>.Evictor>>
TASK_MAP

Keys are weak references to tasks, values are runners managed by executor.

Constructor Summary

AccessConstructor and Description
private
EvictionTimer()

Prevents instantiation

Method Summary

Modifier and TypeMethod and Description
pack-priv static synchronized void
cancel(final BaseGenericObjectPool<?>.Evictor
Task to be cancelled.
evictor
,
final Duration
If the associated executor is no longer required, how long should this thread wait for the executor to terminate?
timeout
,
final boolean
The state of the evictor.
restarting
)

Removes the specified eviction task from the timer.

pack-priv static ScheduledThreadPoolExecutor

Returns:

The executor.
getExecutor
()

For testing only.

pack-priv static synchronized int

Returns:

the number of eviction tasks under management.
getNumTasks
()

pack-priv static HashMap<WeakReference<BaseGenericObjectPool<?>.Evictor>, EvictionTimer.WeakRunner<BaseGenericObjectPool<?>.Evictor>>

Returns:

the task map.
getTaskMap
()

Gets the task map.

private static void
remove(final BaseGenericObjectPool<?>.Evictor
Eviction task to remove
evictor
)

Removes evictor from the task set and executor.

pack-priv static synchronized void
schedule(final BaseGenericObjectPool<?>.Evictor
Task to be scheduled.
task
,
final Duration
Delay in milliseconds before task is executed.
delay
,
final Duration
Time in milliseconds between executions.
period
)

Adds the specified eviction task to the timer.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

executorback to summary
private static ScheduledThreadPoolExecutor executor

Executor instance

TASK_MAPback to summary
private static final HashMap<WeakReference<BaseGenericObjectPool<?>.Evictor>, EvictionTimer.WeakRunner<BaseGenericObjectPool<?>.Evictor>> TASK_MAP

Keys are weak references to tasks, values are runners managed by executor.

Constructor Detail

EvictionTimerback to summary
private EvictionTimer()

Prevents instantiation

Method Detail

cancelback to summary
pack-priv static synchronized void cancel(final BaseGenericObjectPool<?>.Evictor evictor, final Duration timeout, final boolean restarting)

Removes the specified eviction task from the timer.

Parameters
evictor:BaseGenericObjectPool<?>.Evictor

Task to be cancelled.

timeout:Duration

If the associated executor is no longer required, how long should this thread wait for the executor to terminate?

restarting:boolean

The state of the evictor.

getExecutorback to summary
pack-priv static ScheduledThreadPoolExecutor getExecutor()

For testing only.

Returns:ScheduledThreadPoolExecutor

The executor.

getNumTasksback to summary
pack-priv static synchronized int getNumTasks()
Returns:int

the number of eviction tasks under management.

getTaskMapback to summary
pack-priv 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:HashMap<WeakReference<BaseGenericObjectPool<?>.Evictor>, EvictionTimer.WeakRunner<BaseGenericObjectPool<?>.Evictor>>

the task map.

removeback to summary
private static void remove(final BaseGenericObjectPool<?>.Evictor evictor)

Removes evictor from the task set and executor. Only called when holding the class lock.

Parameters
evictor:BaseGenericObjectPool<?>.Evictor

Eviction task to remove

scheduleback to summary
pack-priv static synchronized void schedule(final BaseGenericObjectPool<?>.Evictor task, final Duration delay, final Duration period)

Adds the specified eviction task to the timer. Tasks that are added with a call to this method *must* call cancel(BaseGenericObjectPool.Evictor, Duration, boolean) to cancel the task to prevent memory and/or thread leaks in application server environments.

Parameters
task:BaseGenericObjectPool<?>.Evictor

Task to be scheduled.

delay:Duration

Delay in milliseconds before task is executed.

period:Duration

Time in milliseconds between executions.

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

Annotations
@Override
Since
2.4.3
org.apache.commons.pool2.impl back to summary

private Class EvictionTimer.EvictorThreadFactory

extends Object
implements ThreadFactory
Class Inheritance
  • java.lang.Object
  • org.apache.commons.pool2.impl.EvictionTimer.EvictorThreadFactory
All Implemented Interfaces
java.util.concurrent.ThreadFactory

Thread factory that creates a daemon thread, with the context class loader from this class.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public Thread
newThread(final Runnable
a runnable to be executed by new thread instance
runnable
)

Implements java.util.concurrent.ThreadFactory.newThread.

Constructs a new unstarted Thread to run the given runnable.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

EvictorThreadFactoryback to summary
private EvictorThreadFactory()

Method Detail

newThreadback to summary
public Thread newThread(final Runnable runnable)

Implements java.util.concurrent.ThreadFactory.newThread.

Doc from java.util.concurrent.ThreadFactory.newThread.

Constructs a new unstarted Thread to run the given runnable.

Parameters
runnable:Runnable

a runnable to be executed by new thread instance

Returns:Thread

constructed thread, or null if the request to create a thread is rejected

Annotations
@Override
org.apache.commons.pool2.impl back to summary

private Class EvictionTimer.Reaper

extends Object
implements Runnable
Class Inheritance
All Implemented Interfaces
java.lang.Runnable

Task that removes references to abandoned tasks and shuts down the executor if there are no live tasks left.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public void
run()

Implements java.lang.Runnable.run.

Runs this operation.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

Reaperback to summary
private Reaper()

Method Detail

runback to summary
public void run()

Implements java.lang.Runnable.run.

Doc from java.lang.Runnable.run.

Runs this operation.

Annotations
@Override
org.apache.commons.pool2.impl back to summary

private Class EvictionTimer.WeakRunner<R extends java.lang.Runnable>

extends Object
implements Runnable
Class Inheritance
All Implemented Interfaces
java.lang.Runnable
Type Parameters
<R>
The kind of Runnable.

Runnable that runs the referent of a weak reference. When the referent is no no longer reachable, run is no-op.

Field Summary

Modifier and TypeField and Description
private final WeakReference<R>

Constructor Summary

AccessConstructor and Description
private
WeakRunner(final WeakReference<R>
the reference to track.
ref
)

Constructs a new instance to track the given reference.

Method Summary

Modifier and TypeMethod and Description
public void
run()

Implements java.lang.Runnable.run.

Runs this operation.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

refback to summary
private final WeakReference<R> ref

Constructor Detail

WeakRunnerback to summary
private WeakRunner(final WeakReference<R> ref)

Constructs a new instance to track the given reference.

Parameters
ref:WeakReference<R>

the reference to track.

Method Detail

runback to summary
public void run()

Implements java.lang.Runnable.run.

Doc from java.lang.Runnable.run.

Runs this operation.

Annotations
@Override