T
- the type of resource this pool manages@ThreadSafe public abstract class ResourcePool<T> extends Object implements Pool<T>
FileSystemMasterClientPool
as an example.Modifier and Type | Field and Description |
---|---|
protected AtomicInteger |
mCurrentCapacity
It represents the total number of resources that have been created by this pool.
|
protected int |
mMaxCapacity |
protected ConcurrentLinkedQueue<T> |
mResources |
Modifier | Constructor and Description |
---|---|
|
ResourcePool(int maxCapacity)
Creates a
ResourcePool instance with the specified capacity. |
protected |
ResourcePool(int maxCapacity,
ConcurrentLinkedQueue<T> resources)
Internal constructor that can provide an object to be used for the internal queue.
|
Modifier and Type | Method and Description |
---|---|
T |
acquire()
Acquires an object of type
T from the pool. |
T |
acquire(long time,
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() |
protected final int mMaxCapacity
protected final ConcurrentLinkedQueue<T> mResources
protected final AtomicInteger mCurrentCapacity
public ResourcePool(int maxCapacity)
ResourcePool
instance with the specified capacity.maxCapacity
- the maximum of resources in this poolprotected ResourcePool(int maxCapacity, ConcurrentLinkedQueue<T> resources)
maxCapacity
- bhe maximum of resources in this poolresources
- blocking queue to usepublic T acquire()
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.@Nullable public T acquire(long time, TimeUnit unit)
acquire()
, but it will time out if an object cannot be
acquired before the specified amount of time.public abstract void close() throws IOException
close
in interface Closeable
close
in interface AutoCloseable
IOException
public void release(T resource)
public abstract T createNewResource()
Copyright © 2023. All Rights Reserved.