Class BoundedObjectPool<T>


  • public abstract class BoundedObjectPool<T>
    extends java.lang.Object
    Pool of objects bounded by size where threads may create new objects whenever the pool is empty. Use by subclassing and defining create(), reset(Object), and destroy(Object).
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()
      Removes all objects from the pool, calling destroy(Object) on each.
      protected abstract T create()
      Creates a new object if the pool is empty.
      protected abstract void destroy​(T obj)
      Called on an object which will not be returned to the pool.
      int getMaxPoolSize()
      Returns the maximum pool size, 100 by default.
      T obtain()
      Obtains a new object from the pool.
      void recycle​(T obj)
      Returns an object to the pool.
      protected abstract boolean reset​(T obj)
      Called on an object before it is returned to the pool.
      void setMaxPoolSize​(int maxPoolSize)
      Sets the maximum pool size.
      • Methods inherited from class java.lang.Object

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

      • BoundedObjectPool

        public BoundedObjectPool()
    • Method Detail

      • getMaxPoolSize

        public int getMaxPoolSize()
        Returns the maximum pool size, 100 by default.
        Returns:
        the maximum pool size, 100 by default.
      • setMaxPoolSize

        public void setMaxPoolSize​(int maxPoolSize)
        Sets the maximum pool size.
        Parameters:
        maxPoolSize - the new maximum pool size
      • create

        protected abstract T create()
        Creates a new object if the pool is empty. Implemented by subclasses.
        Returns:
        a new object.
      • reset

        protected abstract boolean reset​(T obj)
        Called on an object before it is returned to the pool. Implemented by subclasses.
        Parameters:
        obj - an object to be reset before returning to the pool.
        Returns:
        whether the object can be returned; if false destroy(Object) will be called.
      • destroy

        protected abstract void destroy​(T obj)
        Called on an object which will not be returned to the pool. Implemented by subclasses.
        Parameters:
        obj - an object which will not be returned to the pool.
      • clear

        public void clear()
        Removes all objects from the pool, calling destroy(Object) on each.
      • obtain

        public T obtain()
        Obtains a new object from the pool. Calls create() to create a new object if the pool is empty.
        Returns:
        a new object from the pool, or else a newly-created object.
      • recycle

        public void recycle​(T obj)
        Returns an object to the pool. Calls reset(Object) before returning the object. If returning the object might cause the pool size to exceed its maximum, the object is not returned to the pool; reset(Object) is not called, and destroy(Object) is called.
        Parameters:
        obj - an object to return to the pool