Class ObjectPool<T>


  • public final class ObjectPool<T>
    extends java.lang.Object
    A pool of objects of type T.

    When an object is borrowed from an empty pool, an object will be created by the supplied objectCreator and returned immediately. When the pool is not empty, an object is removed from the pool and returned. The user is expected to return the object to the pool when it is no longer used.

    This class is internal and is hence not for public use. Its APIs are unstable and can change at any time.

    This class is not thread-safe.

    • Constructor Summary

      Constructors 
      Constructor Description
      ObjectPool​(java.util.function.Supplier<T> objectCreator)
      Constructs an object pool.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      T borrowObject()
      Gets an object from the pool.
      void returnObject​(T object)  
      • Methods inherited from class java.lang.Object

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

      • objectCreator

        private final java.util.function.Supplier<T> objectCreator
    • Constructor Detail

      • ObjectPool

        public ObjectPool​(java.util.function.Supplier<T> objectCreator)
        Constructs an object pool.
        Parameters:
        objectCreator - Supplier used to create an object when the pool is empty
    • Method Detail

      • borrowObject

        public T borrowObject()
        Gets an object from the pool.
        Returns:
        An object from the pool, or a new object if the pool is empty
      • returnObject

        public void returnObject​(T object)