Interface ObjectPool<T>

  • Type Parameters:
    T - Type of element pooled in this pool.
    All Superinterfaces:
    AutoCloseable, Closeable
    All Known Implementing Classes:
    BaseObjectPool, GenericObjectPool, SoftReferenceObjectPool

    public interface ObjectPool<T>
    extends Closeable
    A pooling simple interface.

    Example of use:

     Object obj = null;
    
     try {
         obj = pool.borrowObject();
         try {
             //...use the object...
         } catch(Exception e) {
             // invalidate the object
             pool.invalidateObject(obj);
             // do not return the object to the pool twice
             obj = null;
         } finally {
             // make sure the object is returned to the pool
             if(null != obj) {
                 pool.returnObject(obj);
            }
         }
     } catch(Exception e) {
           // failed to borrow an object
     }

    See BaseObjectPool for a simple base implementation.

    Since:
    2.0
    See Also:
    PooledObjectFactory, KeyedObjectPool, BaseObjectPool
    • Method Summary

      All Methods Instance Methods Abstract Methods Default Methods 
      Modifier and Type Method Description
      void addObject()
      Creates an object using the factory or other implementation dependent mechanism, passivate it, and then place it in the idle object pool.
      default void addObjects​(int count)
      Calls addObject() count number of times.
      T borrowObject()
      Obtains an instance from this pool.
      void clear()
      Clears any objects sitting idle in the pool, releasing any associated resources (optional operation).
      void close()
      Closes this pool, and free any resources associated with it.
      int getNumActive()
      Returns the number of instances currently borrowed from this pool.
      int getNumIdle()
      Returns the number of instances currently idle in this pool.
      void invalidateObject​(T obj)
      Invalidates an object from the pool.
      void returnObject​(T obj)
      Returns an instance to the pool.