Package alluxio.resource
Class ResourcePool<T>
- java.lang.Object
-
- alluxio.resource.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. SeeFileSystemMasterClientPool
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 aResourcePool
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 typeT
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
-
Methods inherited from interface alluxio.resource.Pool
acquireCloseable, acquireCloseable
-
-
-
-
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 aResourcePool
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 poolresources
- blocking queue to use
-
-
Method Detail
-
acquire
public T acquire()
Acquires an object of typeT
from the pool. This operation is blocking if no resource is available. Each call ofacquire()
should be paired with another call ofrelease(Object)
} after the use of this resource completes to return this resource to 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 likeacquire()
, but it will time out if an object cannot be acquired before the specified amount of time.
-
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 interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.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.
-
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
-
-