Class ResourcePool<T>

  • Type Parameters:
    T - the type of resource this pool manages
    All Implemented Interfaces:
    Pool<T>, java.io.Closeable, java.lang.AutoCloseable

    @ThreadSafe
    public abstract class ResourcePool<T>
    extends java.lang.Object
    implements Pool<T>
    Class representing a pool of resources to be temporarily used and returned. Inheriting classes must implement the close method as well as initialize the resources in the constructor. The implemented methods are thread-safe and inheriting classes should also written in a thread-safe manner. See FileSystemMasterClientPool as an example.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected java.util.concurrent.atomic.AtomicInteger mCurrentCapacity
      It represents the total number of resources that have been created by this pool.
      protected int mMaxCapacity  
      protected java.util.concurrent.ConcurrentLinkedQueue<T> mResources  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
        ResourcePool​(int maxCapacity)
      Creates a ResourcePool instance with the specified capacity.
      protected ResourcePool​(int maxCapacity, java.util.concurrent.ConcurrentLinkedQueue<T> resources)
      Internal constructor that can provide an object to be used for the internal queue.
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      T acquire()
      Acquires an object of type T from the pool.
      T acquire​(long time, java.util.concurrent.TimeUnit unit)
      Acquires an object of type {code T} from the pool.
      abstract void close()
      Closes the resource pool.
      abstract T createNewResource()
      Creates a new resource which will be added to the resource pool after the user is done using it.
      void release​(T resource)
      Releases an object of type T, this must be called after the thread is done using a resource obtained by acquire.
      int size()  
      • Methods inherited from class java.lang.Object

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

      • mMaxCapacity

        protected final int mMaxCapacity
      • mResources

        protected final java.util.concurrent.ConcurrentLinkedQueue<T> mResources
      • mCurrentCapacity

        protected final java.util.concurrent.atomic.AtomicInteger mCurrentCapacity
        It represents the total number of resources that have been created by this pool.
    • Constructor Detail

      • ResourcePool

        public ResourcePool​(int maxCapacity)
        Creates a ResourcePool instance with the specified capacity.
        Parameters:
        maxCapacity - the maximum of resources in this pool
      • ResourcePool

        protected ResourcePool​(int maxCapacity,
                               java.util.concurrent.ConcurrentLinkedQueue<T> resources)
        Internal constructor that can provide an object to be used for the internal queue.
        Parameters:
        maxCapacity - bhe maximum of resources in this pool
        resources - blocking queue to use
    • Method Detail

      • acquire

        public T acquire()
        Acquires an object of type T from the pool. This operation is blocking if no resource is available. Each call of acquire() should be paired with another call of release(Object)} after the use of this resource completes to return this resource to the pool.
        Specified by:
        acquire in interface Pool<T>
        Returns:
        a resource taken from the pool
      • acquire

        @Nullable
        public T acquire​(long time,
                         java.util.concurrent.TimeUnit unit)
        Acquires an object of type {code T} from the pool. This method is like acquire(), but it will time out if an object cannot be acquired before the specified amount of time.
        Specified by:
        acquire in interface Pool<T>
        Parameters:
        time - an amount of time to wait, <= 0 to wait indefinitely
        unit - the unit to use for time, null to wait indefinitely
        Returns:
        a resource taken from the pool, or null if the operation times out
      • close

        public abstract void close()
                            throws java.io.IOException
        Closes the resource pool. After this call, the object should be discarded. Inheriting classes should clean up all their resources here.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Throws:
        java.io.IOException
      • release

        public void release​(T resource)
        Releases an object of type T, this must be called after the thread is done using a resource obtained by acquire.
        Specified by:
        release in interface Pool<T>
        Parameters:
        resource - the resource to be released, it should not be reused after calling this method
      • size

        public int size()
        Specified by:
        size in interface Pool<T>
        Returns:
        the current pool size
      • createNewResource

        public abstract T createNewResource()
        Creates a new resource which will be added to the resource pool after the user is done using it. This method should only be called when the capacity of the pool has not been reached. If creating the resource will take a significant amount of time, the inheriting class should avoid calling this method and instead initialize all the resources in the constructor.
        Returns:
        a resource which will be added to the pool of resources