T - the type of objects held in the poolpublic class ConcurrentPool<T> extends Object implements PoolService<T>
ConcurrentCollection guarded by a Semaphore. If the injected
in the pool ConcurrentCollection has native implementation for offerFirst() then
this pool will operate in LIFO mode, otherwise in FIFO mode.
This pool enforces a maximum limit on the number of objects that can be contained or taken out of it at
any time. The pool will lazily create an object upon take request if no ready and valid object
exists in it at the time of the call; not all objects need to exist and be valid in the pool at all times.
The restore(T) methods do not provide any validation whether the currently restored object has been taken
before that from the pool or whether it is in taken state. Correct usage of the restore operations is
established by programming convention in the application.
The pool provides support for fairness with regards to the waiting takers threads.
The creation of new objects and their lifecycle are controlled by the supplied during the
pool creation time PoolObjectFactory. If a Listener instance has been
supplied when instantiating the pool, its methods will be called when the pool executes take
or restore operations.
This pool also provides support for shrinking (reduction) of the number of allocated in it objects.
Note that the shrinking may reduce the createdTotal() to less than the pool initialSize().
The pool cannot contain null objects.
| Constructor and Description |
|---|
ConcurrentPool(ConcurrentCollection<T> available,
PoolObjectFactory<T> poolObjectFactory,
int initialSize,
int maxSize,
boolean fair)
Creates a new
ConcurrentPool with the given PoolObjectFactory, initial and max sizes,
and fairness setting. |
ConcurrentPool(ConcurrentCollection<T> available,
PoolObjectFactory<T> poolObjectFactory,
int initialSize,
int maxSize,
boolean fair,
Listener<T> listener)
Creates a new
ConcurrentPool with the given PoolObjectFactory, initial and max sizes,
and fairness setting. |
| Modifier and Type | Method and Description |
|---|---|
void |
close()
A synonym for
BasePool.terminate(). |
int |
createdTotal()
Returns the total number of created objects which currently exist for this object pool.
|
int |
drainCreated()
Tries to remove (and destroy) as many created objects from this object pool as possible.
|
int |
initialSize()
Returns the
initialSize of this object pool at construction time. |
boolean |
isFair()
Returns the fairness setting of this object pool.
|
boolean |
isTerminated()
Returns the current terminated state of this object pool.
|
Listener<T> |
listener()
Returns the
Listener interface instance associated with this object pool, if any. |
int |
maxSize()
Returns the
maxSize of this object pool. |
int |
reduceCreatedBy(int reduceBy,
boolean ignoreInitialSize)
Tries to remove (and destroy) up to
reduceBy objects from the object pool. |
int |
reduceCreatedTo(int reduceTo,
boolean ignoreInitialSize)
Tries to remove (and destroy) such number of objects from the object pool that the number of
BasePool.createdTotal() objects in the pool to become equal of reduceTo. |
int |
remainingCapacity()
Returns the remaining capacity of this object pool, i.e.
|
int |
remainingCreated()
Returns the number of remaining created objects which are currently available in this object pool.
|
void |
restore(T object)
Restores (returns) an object to the object pool.
|
void |
restore(T object,
boolean valid)
Restores (returns) an object to the object pool.
|
T |
take()
A counterpart of
PoolService.take(long[]) that does not report back the time waited
to obtain an object from the pool. |
T |
take(long[] waitedNanos)
Takes an object from the object pool if there is such available.
|
int |
taken()
Returns the number of objects taken from this object pool.
|
T |
takeUninterruptibly()
A counterpart of
PoolService.takeUninterruptibly(long[]) that does not report back the time waited
to obtain an object from the pool. |
T |
takeUninterruptibly(long[] waitedNanos)
Takes an object from the object pool if there is such available.
|
void |
terminate()
Terminates this object pool.
|
String |
toString() |
T |
tryTake()
Tries to take an object from the object pool if there is one that is immediately available; the object may
need to be created as described in
PoolService.tryTake(long, TimeUnit, long[]). |
T |
tryTake(long timeout,
TimeUnit unit)
A counterpart of
PoolService.tryTake(long, TimeUnit, long[]) that does not report back the time waited
to obtain an object from the pool. |
T |
tryTake(long timeout,
TimeUnit unit,
long[] waitedNanos)
Tries to take an object from the object pool if there is one available.
|
public ConcurrentPool(ConcurrentCollection<T> available, PoolObjectFactory<T> poolObjectFactory, int initialSize, int maxSize, boolean fair)
ConcurrentPool with the given PoolObjectFactory, initial and max sizes,
and fairness setting.available - the concurrent collection that will store the pooled objects;
it must be an empty collection or a collection pre-initialized with
initialSize objectspoolObjectFactory - the factory which will be used to create new objects
in this object pool as well as to control their lifecycleinitialSize - the object pool initial size, i.e. the initial number of
allocated in the object pool objects; this parameter never changesmaxSize - the object pool max size, i.e. the max number of allocated
in the object pool objects; this parameter never changesfair - the object pool fairness setting with regards to waiting threadsIllegalArgumentException - if one of the following holds:initialSize < 0 || maxSize < 1 || maxSize < initialSizeNullPointerException - if available or poolObjectFactory are nullpublic ConcurrentPool(ConcurrentCollection<T> available, PoolObjectFactory<T> poolObjectFactory, int initialSize, int maxSize, boolean fair, Listener<T> listener)
ConcurrentPool with the given PoolObjectFactory, initial and max sizes,
and fairness setting.available - the concurrent collection that will store the pooled objects;
it must be an empty collection or a collection pre-initialized with
initialSize objectspoolObjectFactory - the factory which will be used to create new objects
in this object pool as well as to control their lifecycleinitialSize - the object pool initial size, i.e. the initial number of
allocated in the object pool objects; this parameter never changesmaxSize - the object pool max size, i.e. the max number of allocated
in the object pool objects; this parameter never changesfair - the object pool fairness setting with regards to waiting threadslistener - if not null, this listener instance methods will be called
when the pool executes take or restore operationsIllegalArgumentException - if one of the following holds:initialSize < 0 || maxSize < 1 || maxSize < initialSizeNullPointerException - if available or poolObjectFactory are nullpublic T take()
PoolServicePoolService.take(long[]) that does not report back the time waited
to obtain an object from the pool.take in interface PoolService<T>null if was interrupted while waitingpublic T take(long[] waitedNanos)
PoolServicePoolService.tryTake(long, TimeUnit, long[]). If the calling thread is interrupted
while waiting this call will return null and the thread's interrupted status will
be set to true.take in interface PoolService<T>waitedNanos - used to report the time waited, see PoolService.tryTake(long, TimeUnit, long[])null if it was interrupted while waitingpublic T takeUninterruptibly()
PoolServicePoolService.takeUninterruptibly(long[]) that does not report back the time waited
to obtain an object from the pool.takeUninterruptibly in interface PoolService<T>public T takeUninterruptibly(long[] waitedNanos)
PoolServicePoolService.tryTake(long, TimeUnit, long[]).takeUninterruptibly in interface PoolService<T>waitedNanos - used to report the time waited, see PoolService.tryTake(long, TimeUnit, long[])public T tryTake(long timeout, TimeUnit unit)
PoolServicePoolService.tryTake(long, TimeUnit, long[]) that does not report back the time waited
to obtain an object from the pool.tryTake in interface PoolService<T>timeout - the maximum time to wait for an object to become available in the object pool;
this timeout does not include the object creation time, see aboveunit - the time unit of the timeout argumentnull if the specified timeout expires
or if it was interrupted while waitingpublic T tryTake(long timeout, TimeUnit unit, long[] waitedNanos)
PoolServicetimeout. The real time spent waiting is
reported back via the waitedNanos parameter. The total method execution time may also include the
object creation time - an object can be (lazily) created in the pool when the pool capacity is not reached
yet but no ready and valid object existed in the pool. If the calling thread is interrupted while waiting
this call will return null and the thread's interrupted status will be set to true.tryTake in interface PoolService<T>timeout - the maximum time to wait for an object to become available in the object pool;
this timeout does not include the object creation timeunit - the time unit of the timeout argumentwaitedNanos - this parameter is used to report the nanoseconds time waited for an object to become
available in the pool, excluding any object creation time; the time waited will be stored
at index 0 of this array; the array must be of size of at least onenull if the specified timeout expires
or if it was interrupted while waitingpublic T tryTake()
PoolServicePoolService.tryTake(long, TimeUnit, long[]). Returns null if no object
is available in the pool at the time of the call.tryTake in interface PoolService<T>null if no object was availablepublic void restore(T object)
PoolServicerestore(object, true).restore in interface PoolService<T>object - the object to be restored / returnedpublic void restore(T object, boolean valid)
PoolServicerestore in interface PoolService<T>object - the object to be restored / returnedvalid - if false, the object is treated as invalid; otherwise a secondary validation on the object
will be performedpublic Listener<T> listener()
PoolServiceListener interface instance associated with this object pool, if any.listener in interface PoolService<T>null means no Listener is associated with this object pool.public int taken()
BasePoolBasePool.createdTotal().
Typically used for testing and debugging purposes.public int remainingCreated()
BasePoolBasePool.remainingCapacity().
Typically used for testing and debugging purposes.remainingCreated in interface BasePoolpublic int drainCreated()
BasePoolBasePool.createdTotal() to a number less then its BasePool.initialSize().drainCreated in interface BasePoolpublic int createdTotal()
BasePoolBasePool.taken() + BasePool.remainingCreated().
Typically used for testing and debugging purposes.createdTotal in interface BasePoolpublic int remainingCapacity()
BasePoolBasePool.remainingCreated().
Typically used for testing and debugging purposes.remainingCapacity in interface BasePoolpublic int initialSize()
BasePoolinitialSize of this object pool at construction time.
This parameter never changes.initialSize in interface BasePoolinitialSizepublic int maxSize()
BasePoolmaxSize of this object pool. This parameter never changes.public int reduceCreatedBy(int reduceBy,
boolean ignoreInitialSize)
BasePoolreduceBy objects from the object pool. This method may
bring the object pool BasePool.createdTotal() to a number less then its BasePool.initialSize().reduceCreatedBy in interface BasePoolreduceBy - the desired amount of objects to be removedignoreInitialSize - specifies whether the BasePool.createdTotal() may be
reduced to less than BasePool.initialSize()public int reduceCreatedTo(int reduceTo,
boolean ignoreInitialSize)
BasePoolBasePool.createdTotal() objects in the pool to become equal of reduceTo. This method may bring
the object pool BasePool.createdTotal() to a number smaller than its BasePool.initialSize().reduceCreatedTo in interface BasePoolreduceTo - the desired amount of created objects to remain in the poolignoreInitialSize - specifies whether the BasePool.createdTotal() may be
reduced to less than BasePool.initialSize()public void terminate()
BasePoolpublic void close()
BasePoolBasePool.terminate(). Overrides the AutoCloseable's method in order to overrule
the throwing of a checked Exception.close in interface AutoCloseableclose in interface BasePoolpublic boolean isTerminated()
BasePoolisTerminated in interface BasePooltrue if the object pool is terminated; false otherwisepublic boolean isFair()
PoolServiceisFair in interface PoolService<T>true if the object pool is fair to waiting taker threadsCopyright © 2013-2019 vibur.org. All Rights Reserved.