T - the type of objects held in this object poolpublic class ConcurrentLinkedPool<T> extends Object implements PoolService<T>
ConcurrentLinkedQueue guarded by a Semaphore. This
object pool does not provide any validation whether the currently restored object
has been taken before that from the object pool or whether this object is currently in taken state.
Correct usage of the pool is established by programming convention in the application.
This object pool provides support for fairness with regards to the waiting takers threads.
The creation of new objects and their lifecycle are controlled by a supplied during the
object pool creation time PoolObjectFactory. If a Listener instance has been
supplied when instantiating the pool, its methods will be when the pool executes take
or restore operations.
This object pool has support for shrinking (reduction) of the number of
allocated on the pool objects. Note that the shrinking may reduce the
createdTotal() to less than the pool initialSize().
| Constructor and Description |
|---|
ConcurrentLinkedPool(PoolObjectFactory<T> poolObjectFactory,
int initialSize,
int maxSize,
boolean fair)
Creates a new
ConcurrentLinkedPool with the given
PoolObjectFactory, initial and max sizes, fairness setting. |
ConcurrentLinkedPool(PoolObjectFactory<T> poolObjectFactory,
int initialSize,
int maxSize,
boolean fair,
Listener<T> listener)
Creates a new
ConcurrentLinkedPool with the given
PoolObjectFactory, initial and max sizes, fairness setting. |
| Modifier and Type | Method and Description |
|---|---|
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. |
protected T |
newObject() |
int |
reduceCreated(int reduction,
boolean ignoreInitialSize)
Tries to remove (and destroy) up to
reduction objects from the object pool. |
int |
remainingCapacity()
Returns the remaining capacity of this object pool, i.e.
|
int |
remainingCreated()
Returns the number of remaining created objects which currently exist 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()
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()
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 which is immediately available.
|
T |
tryTake(long timeout,
TimeUnit unit)
Takes an object from the object pool if there is such available.
|
public ConcurrentLinkedPool(PoolObjectFactory<T> poolObjectFactory, int initialSize, int maxSize, boolean fair)
ConcurrentLinkedPool with the given
PoolObjectFactory, initial and max sizes, fairness setting.poolObjectFactory - 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 poolObjectFactory is nullpublic ConcurrentLinkedPool(PoolObjectFactory<T> poolObjectFactory, int initialSize, int maxSize, boolean fair, Listener<T> listener)
ConcurrentLinkedPool with the given
PoolObjectFactory, initial and max sizes, fairness setting.poolObjectFactory - 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 poolObjectFactory is nullpublic T take()
null and the thread's interrupted status will
be set to true.take in interface PoolService<T>null if was interrupted while waitingpublic T takeUninterruptibly()
takeUninterruptibly in interface PoolService<T>public T tryTake(long timeout, TimeUnit unit)
timeout for an object to become available. 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 poolunit - the time unit of the timeout argumentnull if the specified timeout expires
or if it was interrupted while waitingpublic T tryTake()
null if no object is available at the moment of the call.tryTake in interface PoolService<T>null if there is no object available in the object poolprotected T newObject()
public void restore(T object)
restore(Object, true).restore in interface PoolService<T>object - an object to be restored (returned) to this object poolpublic void restore(T object, boolean valid)
restore in interface PoolService<T>object - an object to be restored (returned) to this object poolvalid - if true the restored object is presumed to be in valid (healthy) state,
otherwise it is treated as invalidpublic Listener<T> listener()
Listener 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()
PoolService.createdTotal().
Typically used for testing and debugging purposes.taken in interface PoolService<T>public int remainingCreated()
PoolService.remainingCapacity().
Typically used for testing and debugging purposes.remainingCreated in interface PoolService<T>public int drainCreated()
PoolService.createdTotal() to a number less then its PoolService.initialSize().drainCreated in interface PoolService<T>public int createdTotal()
PoolService.taken() + PoolService.remainingCreated().
Typically used for testing and debugging purposes.createdTotal in interface PoolService<T>public int remainingCapacity()
PoolService.remainingCreated().
Typically used for testing and debugging purposes.remainingCapacity in interface PoolService<T>public int initialSize()
initialSize of this object pool at construction time.
This parameter never changes.initialSize in interface PoolService<T>initialSizepublic int maxSize()
maxSize of this object pool. This parameter never changes.maxSize in interface PoolService<T>maxSizepublic int reduceCreated(int reduction,
boolean ignoreInitialSize)
reduction objects from the object pool.
May bring the object pool PoolService.createdTotal() to a number less then its PoolService.initialSize().reduceCreated in interface PoolService<T>reduction - the desired amount of objects to be removedignoreInitialSize - specifies whether the PoolService.createdTotal() may be
reduced to less than PoolService.initialSize()public void terminate()
terminate in interface PoolService<T>public boolean isTerminated()
isTerminated in interface PoolService<T>true if the object pool is terminatedpublic boolean isFair()
isFair in interface PoolService<T>true if the object pool is fair to waiting taker threadsCopyright © 2013-2016 vibur.org. All Rights Reserved.