Class ConcurrentPool<T>
- Type Parameters:
T- the type of objects held in the pool
- All Implemented Interfaces:
AutoCloseable,BasePool,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 regard 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.
- Author:
- Simeon Malchev
-
Constructor Summary
ConstructorsConstructorDescriptionConcurrentPool(ConcurrentCollection<T> available, PoolObjectFactory<T> poolObjectFactory, int initialSize, int maxSize, boolean fair) Creates a newConcurrentPoolwith the givenPoolObjectFactory, 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 newConcurrentPoolwith the givenPoolObjectFactory, initial and max sizes, and fairness setting. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()A synonym forBasePool.terminate().intReturns the total number of created objects which currently exist for this object pool.intTries to remove (and destroy) as many created objects from this object pool as possible.intReturns theinitialSizeof this object pool at construction time.booleanisFair()Returns the fairness setting of this object pool.booleanReturns the current terminated state of this object pool.listener()Returns theListenerinterface instance associated with this object pool, if any.intmaxSize()Returns themaxSizeof this object pool.intreduceCreatedBy(int reduceBy, boolean ignoreInitialSize) Tries to remove (and destroy) up toreduceByobjects from the object pool.intreduceCreatedTo(int reduceTo, boolean ignoreInitialSize) Tries to remove (and destroy) such number of objects from the object pool that the number ofBasePool.createdTotal()objects in the pool to become equal ofreduceTo.intReturns the remaining capacity of this object pool, i.e. the number of objects which could be taken from this object pool without blocking.intReturns the number of remaining created objects which are currently available in this object pool.voidRestores (returns) an object to the object pool.voidRestores (returns) an object to the object pool.take()A counterpart ofPoolService.take(long[])that does not report back the time waited to obtain an object from the pool.take(long[] waitedNanos) Takes an object from the object pool if there is such available.inttaken()Returns the number of objects taken from this object pool.A counterpart ofPoolService.takeUninterruptibly(long[])that does not report back the time waited to obtain an object from the pool.takeUninterruptibly(long[] waitedNanos) Takes an object from the object pool if there is such available.voidTerminates this object pool.toString()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 inPoolService.tryTake(long, TimeUnit, long[]).A counterpart ofPoolService.tryTake(long, TimeUnit, long[])that does not report back the time waited to obtain an object from the pool.Tries to take an object from the object pool if there is one available.
-
Constructor Details
-
ConcurrentPool
public ConcurrentPool(ConcurrentCollection<T> available, PoolObjectFactory<T> poolObjectFactory, int initialSize, int maxSize, boolean fair) Creates a newConcurrentPoolwith the givenPoolObjectFactory, initial and max sizes, and fairness setting.- Parameters:
available- the concurrent collection that will store the pooled objects; it must be an empty collection or a collection pre-initialized withinitialSizeobjectspoolObjectFactory- 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 regard to waiting threads- Throws:
IllegalArgumentException- if one of the following holds:
initialSize < 0 || maxSize < 1 || maxSize < initialSizeNullPointerException- ifavailableorpoolObjectFactoryare null
-
ConcurrentPool
public ConcurrentPool(ConcurrentCollection<T> available, PoolObjectFactory<T> poolObjectFactory, int initialSize, int maxSize, boolean fair, Listener<T> listener) Creates a newConcurrentPoolwith the givenPoolObjectFactory, initial and max sizes, and fairness setting.- Parameters:
available- the concurrent collection that will store the pooled objects; it must be an empty collection or a collection pre-initialized withinitialSizeobjectspoolObjectFactory- 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 regard to waiting threadslistener- if notnull, this listener instance methods will be called when the pool executestakeorrestoreoperations- Throws:
IllegalArgumentException- if one of the following holds:
initialSize < 0 || maxSize < 1 || maxSize < initialSizeNullPointerException- ifavailableorpoolObjectFactoryare null
-
-
Method Details
-
take
Description copied from interface:PoolServiceA counterpart ofPoolService.take(long[])that does not report back the time waited to obtain an object from the pool.- Specified by:
takein interfacePoolService<T>- Returns:
- an object taken from the object pool or
nullif was interrupted while waiting
-
take
Description copied from interface:PoolServiceTakes an object from the object pool if there is such available. This is a blocking call that waits indefinitely until an object becomes available; the object may need to be created as described inPoolService.tryTake(long, TimeUnit, long[]). If the calling thread is interrupted while waiting this call will returnnulland the thread's interrupted status will be set totrue.- Specified by:
takein interfacePoolService<T>- Parameters:
waitedNanos- used to report the time waited, seePoolService.tryTake(long, TimeUnit, long[])- Returns:
- an object taken from the object pool or
nullif it was interrupted while waiting
-
takeUninterruptibly
Description copied from interface:PoolServiceA counterpart ofPoolService.takeUninterruptibly(long[])that does not report back the time waited to obtain an object from the pool.- Specified by:
takeUninterruptiblyin interfacePoolService<T>- Returns:
- an object taken from the object pool
-
takeUninterruptibly
Description copied from interface:PoolServiceTakes an object from the object pool if there is such available. This is a blocking call that waits indefinitely until an object becomes available; the object may need to be created as described inPoolService.tryTake(long, TimeUnit, long[]).- Specified by:
takeUninterruptiblyin interfacePoolService<T>- Parameters:
waitedNanos- used to report the time waited, seePoolService.tryTake(long, TimeUnit, long[])- Returns:
- an object taken from the object pool
-
tryTake
Description copied from interface:PoolServiceA counterpart ofPoolService.tryTake(long, TimeUnit, long[])that does not report back the time waited to obtain an object from the pool.- Specified by:
tryTakein interfacePoolService<T>- Parameters:
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 thetimeoutargument- Returns:
- an object taken from the object pool or
nullif the specified timeout expires or if it was interrupted while waiting
-
tryTake
Description copied from interface:PoolServiceTries to take an object from the object pool if there is one available. This is a blocking call that waits for an object to become available up to the specifiedtimeout. The real time spent waiting is reported back via thewaitedNanosparameter. 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 returnnulland the thread's interrupted status will be set totrue.- Specified by:
tryTakein interfacePoolService<T>- Parameters:
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 thetimeoutargumentwaitedNanos- 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 index0of this array; the array must be of size of at least one- Returns:
- an object taken from the object pool or
nullif the specified timeout expires or if it was interrupted while waiting
-
tryTake
Description copied from interface:PoolServiceTries 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 inPoolService.tryTake(long, TimeUnit, long[]). Returnsnullif no object is available in the pool at the time of the call.- Specified by:
tryTakein interfacePoolService<T>- Returns:
- an object from the object pool or
nullif no object was available
-
restore
Description copied from interface:PoolServiceRestores (returns) an object to the object pool. The object pool does not verify whether the currently restored object has been taken before that from this object pool or whether it is currently in taken state. Equivalent to callingrestore(object, true).- Specified by:
restorein interfacePoolService<T>- Parameters:
object- the object to be restored / returned
-
restore
Description copied from interface:PoolServiceRestores (returns) an object to the object pool. The object pool does not verify whether the currently restored object has been taken before that from this object pool or whether it is currently in taken state.- Specified by:
restorein interfacePoolService<T>- Parameters:
object- the object to be restored / returnedvalid- iffalse, the object is treated as invalid; otherwise a secondary validation on the object will be performed
-
listener
Description copied from interface:PoolServiceReturns theListenerinterface instance associated with this object pool, if any.- Specified by:
listenerin interfacePoolService<T>- Returns:
- see above;
nullmeans noListeneris associated with this object pool.
-
taken
public int taken()Description copied from interface:BasePoolReturns the number of objects taken from this object pool. This number is less than or equal to the object poolBasePool.createdTotal(). Typically used for testing and debugging purposes. -
remainingCreated
public int remainingCreated()Description copied from interface:BasePoolReturns the number of remaining created objects which are currently available in this object pool. This number is less than or equal to the object poolBasePool.remainingCapacity(). Typically used for testing and debugging purposes.- Specified by:
remainingCreatedin interfaceBasePool- Returns:
- the number of remaining created objects in this object pool
-
drainCreated
public int drainCreated()Description copied from interface:BasePoolTries to remove (and destroy) as many created objects from this object pool as possible. May bring the object poolBasePool.createdTotal()to a number less than itsBasePool.initialSize().- Specified by:
drainCreatedin interfaceBasePool- Returns:
- the actual amount of objects removed (and destroyed)
-
toString
-
createdTotal
public int createdTotal()Description copied from interface:BasePoolReturns the total number of created objects which currently exist for this object pool. This number is equal toBasePool.taken()+BasePool.remainingCreated(). Typically used for testing and debugging purposes.- Specified by:
createdTotalin interfaceBasePool- Returns:
- the total number of created objects for this object pool
-
remainingCapacity
public int remainingCapacity()Description copied from interface:BasePoolReturns the remaining capacity of this object pool, i.e. the number of objects which could be taken from this object pool without blocking. It is not guaranteed that all these objects exist at the time of the call (i.e. are already created) in the object pool - some of them might be created on demand upon take requests. Also seeBasePool.remainingCreated(). Typically used for testing and debugging purposes.- Specified by:
remainingCapacityin interfaceBasePool- Returns:
- the object pool remaining capacity
-
initialSize
public int initialSize()Description copied from interface:BasePoolReturns theinitialSizeof this object pool at construction time. This parameter never changes.- Specified by:
initialSizein interfaceBasePool- Returns:
- the object pool
initialSize
-
maxSize
public int maxSize()Description copied from interface:BasePoolReturns themaxSizeof this object pool. This parameter never changes. -
reduceCreatedBy
public int reduceCreatedBy(int reduceBy, boolean ignoreInitialSize) Description copied from interface:BasePoolTries to remove (and destroy) up toreduceByobjects from the object pool. This method may bring the object poolBasePool.createdTotal()to a number less than itsBasePool.initialSize().- Specified by:
reduceCreatedByin interfaceBasePool- Parameters:
reduceBy- the desired amount of objects to be removedignoreInitialSize- specifies whether theBasePool.createdTotal()may be reduced to less thanBasePool.initialSize()- Returns:
- the actual amount of objects removed
-
reduceCreatedTo
public int reduceCreatedTo(int reduceTo, boolean ignoreInitialSize) Description copied from interface:BasePoolTries to remove (and destroy) such number of objects from the object pool that the number ofBasePool.createdTotal()objects in the pool to become equal ofreduceTo. This method may bring the object poolBasePool.createdTotal()to a number smaller than itsBasePool.initialSize().- Specified by:
reduceCreatedToin interfaceBasePool- Parameters:
reduceTo- the desired amount of created objects to remain in the poolignoreInitialSize- specifies whether theBasePool.createdTotal()may be reduced to less thanBasePool.initialSize()- Returns:
- the actual amount of objects removed
-
terminate
public void terminate()Description copied from interface:BasePoolTerminates this object pool. Once terminated the object pool cannot be more revived. All take and restore operations called on a terminated object pool should throw an exception or be ignored. This method can be invoked multiple times. -
close
public void close()Description copied from interface:BasePoolA synonym forBasePool.terminate(). Overrides theAutoCloseable's method in order to overrule the throwing of a checkedException.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceBasePool
-
isTerminated
public boolean isTerminated()Description copied from interface:BasePoolReturns the current terminated state of this object pool.- Specified by:
isTerminatedin interfaceBasePool- Returns:
trueif the object pool is terminated; false otherwise
-
isFair
public boolean isFair()Description copied from interface:PoolServiceReturns the fairness setting of this object pool.- Specified by:
isFairin interfacePoolService<T>- Returns:
trueif the object pool is fair to waiting taker threads
-