Package com.helger.commons.pool
Class ObjectPool<DATATYPE>
java.lang.Object
com.helger.commons.pool.ObjectPool<DATATYPE>
- Type Parameters:
DATATYPE- The type of the objects contained in the pool.
- All Implemented Interfaces:
IHasConditionalLogger,IMutableObjectPool<DATATYPE>
@ThreadSafe
public final class ObjectPool<DATATYPE>
extends Object
implements IMutableObjectPool<DATATYPE>, IHasConditionalLogger
A simple generic object pool with a fixed size determined in the constructor.
- Author:
- Philip Helger
-
Constructor Summary
ConstructorsConstructorDescriptionObjectPool(int nItemCount, IObjectPoolFactory<DATATYPE> aFactory) Create a new object pool for a certain amount of items and a factory that creates the objects on demand.ObjectPool(int nItemCount, Supplier<? extends DATATYPE> aFactory) Create a new object pool for a certain amount of items and a factory that creates the objects on demand. -
Method Summary
Modifier and TypeMethodDescriptionBorrow an object from the pool.voidintintstatic booleanreturnObject(DATATYPE aItem) Return a previously borrowed object back to the pool.static booleansetSilentMode(boolean bSilentMode) Enable or disable certain regular log messages.toString()
-
Constructor Details
-
ObjectPool
Create a new object pool for a certain amount of items and a factory that creates the objects on demand.- Parameters:
nItemCount- The number of items in the pool. Must be ≥ 1.aFactory- The factory to create object. May not benull. The factory may not createnullobjects, as this leads to an error!
-
ObjectPool
Create a new object pool for a certain amount of items and a factory that creates the objects on demand.- Parameters:
nItemCount- The number of items in the pool. Must be ≥ 1.aFactory- The factory to create object. May not benull. The factory may not createnullobjects, as this leads to an error!
-
-
Method Details
-
isSilentMode
public static boolean isSilentMode()- Returns:
trueif logging is disabled,falseif it is enabled.- Since:
- 9.4.0
-
setSilentMode
public static boolean setSilentMode(boolean bSilentMode) Enable or disable certain regular log messages.- Parameters:
bSilentMode-trueto disable logging,falseto enable logging- Returns:
- The previous value of the silent mode.
- Since:
- 9.4.0
-
getPoolSize
- Returns:
- The maximum number of items in the pool. Always > 0.
- Since:
- 11.0.6
- See Also:
-
getBorrowedObjectCount
- Returns:
- The number of objects currently borrowed from the pool. Something
between 0 and
getPoolSize() - Since:
- 11.0.6
- See Also:
-
clearUnusedItems
public void clearUnusedItems() -
borrowObject
Description copied from interface:IMutableObjectPoolBorrow an object from the pool. This method blocks until an object is available.- Specified by:
borrowObjectin interfaceIMutableObjectPool<DATATYPE>- Returns:
- The borrowed object. May be
nulldepending on the factory, and the locking used.
-
returnObject
Description copied from interface:IMutableObjectPoolReturn a previously borrowed object back to the pool.- Specified by:
returnObjectin interfaceIMutableObjectPool<DATATYPE>- Parameters:
aItem- The previously borrowed object to be returned. Nevernull.- Returns:
ESuccess.SUCCESSupon success
-
toString
-