Class BoundedResourcePool<T>

java.lang.Object
org.apache.hadoop.fs.impl.prefetch.ResourcePool<T>
org.apache.hadoop.fs.impl.prefetch.BoundedResourcePool<T>
All Implemented Interfaces:
Closeable, AutoCloseable

public abstract class BoundedResourcePool<T> extends ResourcePool<T>
Manages a fixed pool of resources. Avoids creating a new resource if a previously created instance is already available.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a resource pool of the given size.
  • Method Summary

    Modifier and Type
    Method
    Description
    Acquires a resource blocking if necessary until one becomes available.
    void
     
    protected void
    close(T item)
    Derived classes may implement a way to cleanup each item.
    protected abstract T
    Derived classes must implement a way to create an instance of a resource.
    int
    Number of items available to be acquired.
    int
    Number of items created so far.
    void
    release(T item)
    Releases a previously acquired resource.
     
    Acquires a resource blocking if one is immediately available.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • BoundedResourcePool

      public BoundedResourcePool(int size)
      Constructs a resource pool of the given size.
      Parameters:
      size - the size of this pool. Cannot be changed post creation.
      Throws:
      IllegalArgumentException - if size is zero or negative.
  • Method Details

    • acquire

      public T acquire()
      Acquires a resource blocking if necessary until one becomes available.
      Specified by:
      acquire in class ResourcePool<T>
      Returns:
      the acquired resource instance.
    • tryAcquire

      public T tryAcquire()
      Acquires a resource blocking if one is immediately available. Otherwise returns null.
      Specified by:
      tryAcquire in class ResourcePool<T>
      Returns:
      the acquired resource instance (if immediately available) or null.
    • release

      public void release(T item)
      Releases a previously acquired resource.
      Specified by:
      release in class ResourcePool<T>
      Parameters:
      item - the resource to release.
      Throws:
      IllegalArgumentException - if item is null.
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class ResourcePool<T>
    • close

      protected void close(T item)
      Derived classes may implement a way to cleanup each item.
      Overrides:
      close in class ResourcePool<T>
      Parameters:
      item - the resource to close.
    • numCreated

      public int numCreated()
      Number of items created so far. Mostly for testing purposes.
      Returns:
      the count.
    • numAvailable

      public int numAvailable()
      Number of items available to be acquired. Mostly for testing purposes.
      Returns:
      the number available.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • createNew

      protected abstract T createNew()
      Derived classes must implement a way to create an instance of a resource.
      Specified by:
      createNew in class ResourcePool<T>
      Returns:
      the created instance.