Package org.vibur.objectpool.util
Class SamplingPoolReducer
java.lang.Object
org.vibur.objectpool.util.SamplingPoolReducer
- All Implemented Interfaces:
ThreadedPoolReducer
A sampling pool reducer util, which is awakened up a given number of times during
a predefined period of time, and checks whether the number of available
allocated objects in the object pool needs to be reduced.
This pool reducer will not bring the number of allocated on the pool
objects to less than the pool initial size.
This pool reducer creates one daemon service thread which will be started when
the reducer's start() method is called, and will be alive until the
terminate() method is called or until the calling application exits.
Note that if an exception is thrown by the overridable
afterReduce(int, int, Throwable) method hook, it will terminate the
SamplingPoolReducer, including the reducer's background daemon thread.
- Author:
- Simeon Malchev
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final doubleprotected int -
Constructor Summary
ConstructorsConstructorDescriptionSamplingPoolReducer(BasePool pool, long timeInterval, TimeUnit unit, int samples) -
Method Summary
Modifier and TypeMethodDescriptionprotected voidafterReduce(int reduction, int reduced, Throwable thrown) An after reduce pool hook.protected intCalculates the number of currently allocated on the pool elements that needs to be destroyed/deallocated, as a result of the stats collected during the just finished observational time period.getState()Returns the state of the underlying thread.protected Stringprotected voidprotected voidvoidstart()Starts this pool reducer, which starts its underlying daemon thread.voidTerminates this pool reducer, which terminates its underlying daemon thread.
-
Field Details
-
MAX_REDUCTION_FRACTION
protected static final double MAX_REDUCTION_FRACTION- See Also:
-
minRemainingCreated
protected int minRemainingCreated
-
-
Constructor Details
-
SamplingPoolReducer
Creates a newSamplingPoolReducerwith the givenBasePoolandtimeIntervalsettings. The created pool reducer is not started and needs to be explicitly started via calling thestart()method.- Parameters:
pool- the pool that is to be reduced if necessarytimeInterval- the time period after which theSamplingPoolReducerwill try to possibly reduce the number of created but unused objects in the givenpoolunit- the time unit of thetimeIntervalargumentsamples- how many times theSamplingPoolReducerwill wake up during the giventimeIntervalperiod in order to sample various information from the givenpool- Throws:
IllegalArgumentException- if one of the following holds:
timeInterval <= 0 || samples <= 0NullPointerException- if one of the following holds:
pool == null || unit == null
-
-
Method Details
-
start
public void start()Description copied from interface:ThreadedPoolReducerStarts this pool reducer, which starts its underlying daemon thread.- Specified by:
startin interfaceThreadedPoolReducer
-
getThreadName
-
samplePool
protected void samplePool() -
reducePool
protected void reducePool() -
calculateReduction
protected int calculateReduction()Calculates the number of currently allocated on the pool elements that needs to be destroyed/deallocated, as a result of the stats collected during the just finished observational time period. The number of remaining allocated on the pool objects will not fall below the poolinitialsize as a result of this reduction.- Returns:
- the calculated reduction number
-
afterReduce
An after reduce pool hook. The default implementation willterminate()this pool reducer ifthrown != null. Note that if this method throws an exception, this will terminate the pool reducer, too.- Parameters:
reduction- the intended reduction numberreduced- the number of objects which were successfully removed/destroyed from the poolthrown- a thrown during the pool reduction exception if any.
-
getState
Description copied from interface:ThreadedPoolReducerReturns the state of the underlying thread.- Specified by:
getStatein interfaceThreadedPoolReducer
-
terminate
public void terminate()Description copied from interface:ThreadedPoolReducerTerminates this pool reducer, which terminates its underlying daemon thread. Once terminated the pool reducer cannot be more revived.- Specified by:
terminatein interfaceThreadedPoolReducer
-