Class SoftReferenceObjectPool<T>
- Type Parameters:
T- Type of element pooled in this pool.
- All Implemented Interfaces:
Closeable, AutoCloseable, ObjectPool<T>
- Since:
- 2.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final ArrayList<PooledSoftReference<T>> All references - checked out or waiting to be borrowed.private longTotal number of instances that have been createdprivate longTotal number of instances that have been destroyedprivate final PooledObjectFactory<T> Factory to source pooled objectsprivate final LinkedBlockingDeque<PooledSoftReference<T>> Idle references - waiting to be borrowedprivate intCount of instances that have been checkout out to pool clientsprivate final ReferenceQueue<T> Queue of broken references that might be able to be removed from_pool. -
Constructor Summary
ConstructorsConstructorDescriptionSoftReferenceObjectPool(PooledObjectFactory<T> factory) Create aSoftReferenceObjectPoolwith the specified factory. -
Method Summary
Modifier and TypeMethodDescriptionvoidCreates an object, and places it into the pool.Borrows an object from the pool.voidclear()Clears any objects sitting idle in the pool.voidclose()Closes this pool, and frees any resources associated with it.private voiddestroy(PooledSoftReference<T> toDestroy) Destroys aPooledSoftReferenceand removes it from the idle and all references pools.private PooledSoftReference<T> findReference(T obj) Finds the PooledSoftReference in allReferences that points to obj.Returns thePooledObjectFactoryused by this pool to create and manage object instances.intReturns the number of instances currently borrowed from this pool.intReturns an approximation not less than the of the number of idle instances in the pool.voidinvalidateObject(T obj) Invalidates an object from the pool.private voidIf any idle objects were garbage collected, remove theirReferencewrappers from the idle object pool.private voidremoveClearedReferences(Iterator<PooledSoftReference<T>> iterator) Clears cleared references from iterator's collectionvoidreturnObject(T obj) Returns an instance to the pool after successful validation and passivation.protected voidtoStringAppendFields(StringBuilder builder) Used by sub-classes to include the fields defined by the sub-class in theBaseObject.toString()output.Methods inherited from class BaseObjectPool
assertOpen, isClosedMethods inherited from class BaseObject
toStringMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface ObjectPool
addObjects
-
Field Details
-
factory
Factory to source pooled objects -
refQueue
Queue of broken references that might be able to be removed from_pool. This is used to helpgetNumIdle()be more accurate with minimal performance overhead. -
numActive
private int numActiveCount of instances that have been checkout out to pool clients -
destroyCount
private long destroyCountTotal number of instances that have been destroyed -
createCount
private long createCountTotal number of instances that have been created -
idleReferences
Idle references - waiting to be borrowed -
allReferences
All references - checked out or waiting to be borrowed.
-
-
Constructor Details
-
SoftReferenceObjectPool
Create aSoftReferenceObjectPoolwith the specified factory.- Parameters:
factory- object factory to use.
-
-
Method Details
-
borrowObject
Borrows an object from the pool. If there are no idle instances available in the pool, the configured factory'sPooledObjectFactory.makeObject()method is invoked to create a new instance.All instances are
activatedandvalidatedbefore being returned by this method. If validation fails or an exception occurs activating or validating an idle instance, the failing instance isdestroyedand another instance is retrieved from the pool, validated and activated. This process continues until either the pool is empty or an instance passes validation. If the pool is empty on activation or it does not contain any valid instances, the factory'smakeObjectmethod is used to create a new instance. If the created instance either raises an exception on activation or fails validation,NoSuchElementExceptionis thrown. Exceptions thrown byMakeObjectare propagated to the caller; but other thanThreadDeathorVirtualMachineError, exceptions generated by activation, validation or destroy methods are swallowed silently.- Specified by:
borrowObjectin interfaceObjectPool<T>- Specified by:
borrowObjectin classBaseObjectPool<T>- Returns:
- a valid, activated object instance
- Throws:
NoSuchElementException- if a valid object cannot be providedIllegalStateException- if invoked on aclosedpoolException- if an exception occurs creating a new instance
-
returnObject
Returns an instance to the pool after successful validation and passivation. The returning instance is destroyed if any of the following are true:- the pool is closed
validationfailspassivationthrows an exception
- Specified by:
returnObjectin interfaceObjectPool<T>- Specified by:
returnObjectin classBaseObjectPool<T>- Parameters:
obj- instance to return to the pool- Throws:
IllegalArgumentException- if obj is not currently part of this poolException- if an instance cannot be returned to the pool
-
invalidateObject
Invalidates an object from the pool.By contract,
objmust have been obtained usingObjectPool.borrowObject()or a related method as defined in an implementation or sub-interface.This method should be used when an object that has been borrowed is determined (due to an exception or other problem) to be invalid.
- Specified by:
invalidateObjectin interfaceObjectPool<T>- Specified by:
invalidateObjectin classBaseObjectPool<T>- Parameters:
obj- aborrowedinstance to be disposed.- Throws:
Exception- if the instance cannot be invalidated
-
addObject
Creates an object, and places it into the pool. addObject() is useful for "pre-loading" a pool with idle objects.Before being added to the pool, the newly created instance is
validatedandpassivated. If validation fails, the new instance isdestroyed. Exceptions generated by the factorymakeObjectorpassivateare propagated to the caller. Exceptions destroying instances are silently swallowed.- Specified by:
addObjectin interfaceObjectPool<T>- Overrides:
addObjectin classBaseObjectPool<T>- Throws:
IllegalStateException- if invoked on aclosedpoolException- when thefactoryhas a problem creating or passivating an object.
-
getNumIdle
public int getNumIdle()Returns an approximation not less than the of the number of idle instances in the pool.- Specified by:
getNumIdlein interfaceObjectPool<T>- Overrides:
getNumIdlein classBaseObjectPool<T>- Returns:
- estimated number of idle instances in the pool
-
getNumActive
public int getNumActive()Returns the number of instances currently borrowed from this pool.- Specified by:
getNumActivein interfaceObjectPool<T>- Overrides:
getNumActivein classBaseObjectPool<T>- Returns:
- the number of instances currently borrowed from this pool
-
clear
public void clear()Clears any objects sitting idle in the pool.- Specified by:
clearin interfaceObjectPool<T>- Overrides:
clearin classBaseObjectPool<T>
-
close
public void close()Closes this pool, and frees any resources associated with it. Invokesclear()to destroy and remove instances in the pool.Calling
addObject()orborrowObject()after invoking this method on a pool will cause them to throw anIllegalStateException.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceObjectPool<T>- Overrides:
closein classBaseObjectPool<T>
-
getFactory
Returns thePooledObjectFactoryused by this pool to create and manage object instances.- Returns:
- the factory
-
pruneClearedReferences
private void pruneClearedReferences()If any idle objects were garbage collected, remove theirReferencewrappers from the idle object pool. -
findReference
Finds the PooledSoftReference in allReferences that points to obj.- Parameters:
obj- returning object- Returns:
- PooledSoftReference wrapping a soft reference to obj
-
destroy
Destroys aPooledSoftReferenceand removes it from the idle and all references pools.- Parameters:
toDestroy- PooledSoftReference to destroy- Throws:
Exception- If an error occurs while trying to destroy the object
-
removeClearedReferences
Clears cleared references from iterator's collection- Parameters:
iterator- iterator over idle/allReferences
-
toStringAppendFields
Description copied from class:BaseObjectUsed by sub-classes to include the fields defined by the sub-class in theBaseObject.toString()output.- Overrides:
toStringAppendFieldsin classBaseObjectPool<T>- Parameters:
builder- Field names and values are appended to this object
-