Class ObjectPool<T>

    • Field Summary

      Fields 
      Modifier and Type Field Description
      (package private) int maxSize  
      (package private) int minSize  
      private java.util.concurrent.atomic.AtomicReference<java.util.concurrent.Future<?>> periodicTask  
      (package private) java.util.Queue<T> pool  
      private long validationInterval  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
        ObjectPool()  
      private ObjectPool​(int min, int max, long validationInterval)
      Creates the pool.
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      T borrowObject()
      Gets the next free object from the pool.
      protected abstract T createObject()
      Creates a new object.
      private void initialize​(int min)  
      void returnObject​(T object)
      Returns object back to the pool.
      void shutdown()
      Shutdown this pool.
      void start()
      Allows the Scheduler instance to start threads and accept tasks on them.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • pool

        java.util.Queue<T> pool
      • minSize

        final int minSize
      • maxSize

        final int maxSize
      • validationInterval

        private final long validationInterval
      • periodicTask

        private final java.util.concurrent.atomic.AtomicReference<java.util.concurrent.Future<?>> periodicTask
    • Constructor Detail

      • ObjectPool

        public ObjectPool()
      • ObjectPool

        private ObjectPool​(int min,
                           int max,
                           long validationInterval)
        Creates the pool.
        Parameters:
        minIdle - minimum number of objects residing in the pool
        maxIdle - maximum number of objects residing in the pool
        validationInterval - time in seconds for periodical checking of minIdle / maxIdle conditions in a separate thread. When the number of objects is less than minIdle, missing instances will be created. When the number of objects is greater than maxIdle, too many instances will be removed.
    • Method Detail

      • borrowObject

        public T borrowObject()
        Gets the next free object from the pool. If the pool doesn't contain any objects, a new object will be created and given to the caller of this method back.
        Returns:
        T borrowed object
      • returnObject

        public void returnObject​(T object)
        Returns object back to the pool.
        Parameters:
        object - object to be returned
      • start

        public void start()
        Description copied from interface: SchedulerLifecycle
        Allows the Scheduler instance to start threads and accept tasks on them.

        Implementations should make sure the call is idempotent and threadsafe.

        Specified by:
        start in interface SchedulerLifecycle
      • createObject

        protected abstract T createObject()
        Creates a new object.
        Returns:
        T new object
      • initialize

        private void initialize​(int min)