Class SamplingPoolReducer

java.lang.Object
org.vibur.objectpool.util.SamplingPoolReducer
All Implemented Interfaces:
ThreadedPoolReducer

public class SamplingPoolReducer extends Object implements 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 Details

    • MAX_REDUCTION_FRACTION

      protected static final double MAX_REDUCTION_FRACTION
      See Also:
    • minRemainingCreated

      protected int minRemainingCreated
  • Constructor Details

    • SamplingPoolReducer

      public SamplingPoolReducer(BasePool pool, long timeInterval, TimeUnit unit, int samples)
      Creates a new SamplingPoolReducer with the given BasePool and timeInterval settings. The created pool reducer is not started and needs to be explicitly started via calling the start() method.
      Parameters:
      pool - the pool that is to be reduced if necessary
      timeInterval - the time period after which the SamplingPoolReducer will try to possibly reduce the number of created but unused objects in the given pool
      unit - the time unit of the timeInterval argument
      samples - how many times the SamplingPoolReducer will wake up during the given timeInterval period in order to sample various information from the given pool
      Throws:
      IllegalArgumentException - if one of the following holds:
      timeInterval <= 0 || samples <= 0
      NullPointerException - if one of the following holds:
      pool == null || unit == null
  • Method Details

    • start

      public void start()
      Description copied from interface: ThreadedPoolReducer
      Starts this pool reducer, which starts its underlying daemon thread.
      Specified by:
      start in interface ThreadedPoolReducer
    • getThreadName

      protected String 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 pool initial size as a result of this reduction.
      Returns:
      the calculated reduction number
    • afterReduce

      protected void afterReduce(int reduction, int reduced, Throwable thrown)
      An after reduce pool hook. The default implementation will terminate() this pool reducer if thrown != null. Note that if this method throws an exception, this will terminate the pool reducer, too.
      Parameters:
      reduction - the intended reduction number
      reduced - the number of objects which were successfully removed/destroyed from the pool
      thrown - a thrown during the pool reduction exception if any.
    • getState

      public Thread.State getState()
      Description copied from interface: ThreadedPoolReducer
      Returns the state of the underlying thread.
      Specified by:
      getState in interface ThreadedPoolReducer
    • terminate

      public void terminate()
      Description copied from interface: ThreadedPoolReducer
      Terminates this pool reducer, which terminates its underlying daemon thread. Once terminated the pool reducer cannot be more revived.
      Specified by:
      terminate in interface ThreadedPoolReducer