T - Type of element pooled in this pool.public class SoftReferenceObjectPool<T> extends BaseObjectPool<T>
| Modifier and Type | Field and Description |
|---|---|
private java.util.ArrayList<PooledSoftReference<T>> |
allReferences
All references - checked out or waiting to be borrowed.
|
private long |
createCount
Total number of instances that have been created
|
private long |
destroyCount
Total number of instances that have been destroyed
|
private PooledObjectFactory<T> |
factory
Factory to source pooled objects
|
private LinkedBlockingDeque<PooledSoftReference<T>> |
idleReferences
Idle references - waiting to be borrowed
|
private int |
numActive
Count of instances that have been checkout out to pool clients
|
private java.lang.ref.ReferenceQueue<T> |
refQueue
Queue of broken references that might be able to be removed from
_pool. |
| Constructor and Description |
|---|
SoftReferenceObjectPool(PooledObjectFactory<T> factory)
Create a
SoftReferenceObjectPool with the specified factory. |
| Modifier and Type | Method and Description |
|---|---|
void |
addObject()
Create an object, and place it into the pool.
|
T |
borrowObject()
Borrow an object from the pool.
|
void |
clear()
Clears any objects sitting idle in the pool.
|
void |
close()
Close this pool, and free any resources associated with it.
|
private void |
destroy(PooledSoftReference<T> toDestroy)
Destroy a
PooledSoftReference and remove it from the idle and all
references pools. |
private PooledSoftReference<T> |
findReference(T obj)
Find the PooledSoftReference in allReferences that points to obj.
|
PooledObjectFactory<T> |
getFactory()
Returns the
PooledObjectFactory used by this pool to create and
manage object instances. |
int |
getNumActive()
Return the number of instances currently borrowed from this pool.
|
int |
getNumIdle()
Returns an approximation not less than the of the number of idle
instances in the pool.
|
void |
invalidateObject(T obj)
Invalidates an object from the pool.
|
private void |
pruneClearedReferences()
If any idle objects were garbage collected, remove their
Reference wrappers from the idle object pool. |
private void |
removeClearedReferences(java.util.Iterator<PooledSoftReference<T>> iterator)
Clears cleared references from iterator's collection
|
void |
returnObject(T obj)
Returns an instance to the pool after successful validation and
passivation.
|
assertOpen, isClosedprivate final PooledObjectFactory<T> factory
private final java.lang.ref.ReferenceQueue<T> refQueue
_pool. This is used to help getNumIdle() be more
accurate with minimal performance overhead.private int numActive
private long destroyCount
private long createCount
private final LinkedBlockingDeque<PooledSoftReference<T>> idleReferences
private final java.util.ArrayList<PooledSoftReference<T>> allReferences
public SoftReferenceObjectPool(PooledObjectFactory<T> factory)
SoftReferenceObjectPool with the specified factory.factory - object factory to use.public T borrowObject() throws java.lang.Exception
PooledObjectFactory.makeObject() method is invoked to create a
new instance.
All instances are activated
and validated before being returned by this method. If validation fails or
an exception occurs activating or validating an idle instance, the
failing instance is destroyed and 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's makeObject
method is used to create a new instance. If the created instance either
raises an exception on activation or fails validation,
NoSuchElementException is thrown. Exceptions thrown by
MakeObject are propagated to the caller; but other than
ThreadDeath or VirtualMachineError, exceptions
generated by activation, validation or destroy methods are swallowed
silently.
borrowObject in interface ObjectPool<T>borrowObject in class BaseObjectPool<T>java.util.NoSuchElementException - if a valid object cannot be providedjava.lang.IllegalStateException - if invoked on a closed pooljava.lang.Exception - if an exception occurs creating a new instancepublic void returnObject(T obj) throws java.lang.Exception
validation fails
passivation
throws an exceptionreturnObject in interface ObjectPool<T>returnObject in class BaseObjectPool<T>obj - instance to return to the pooljava.lang.IllegalStateException - if an attempt is made to return an object to the pool that
is in any state other than allocated (i.e. borrowed).
Attempting to return an object more than once or attempting
to return an object that was never borrowed from the pool
will trigger this exception.java.lang.Exception - if an instance cannot be returned to the poolpublic void invalidateObject(T obj) throws java.lang.Exception
By contract, obj must have been obtained
using ObjectPool.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.
invalidateObject in interface ObjectPool<T>invalidateObject in class BaseObjectPool<T>obj - a borrowed instance to be disposed.java.lang.Exception - if the instance cannot be invalidatedpublic void addObject()
throws java.lang.Exception
Before being added to the pool, the newly created instance is
validated and
passivated. If
validation fails, the new instance is
destroyed. Exceptions
generated by the factory makeObject or
passivate are propagated to the caller. Exceptions
destroying instances are silently swallowed.
addObject in interface ObjectPool<T>addObject in class BaseObjectPool<T>java.lang.IllegalStateException - if invoked on a closed pooljava.lang.Exception - when the factory has a problem creating
or passivating an object.public int getNumIdle()
getNumIdle in interface ObjectPool<T>getNumIdle in class BaseObjectPool<T>public int getNumActive()
getNumActive in interface ObjectPool<T>getNumActive in class BaseObjectPool<T>public void clear()
clear in interface ObjectPool<T>clear in class BaseObjectPool<T>public void close()
clear() to destroy and remove instances in the pool.
Calling addObject() or borrowObject() after invoking this
method on a pool will cause them to throw an
IllegalStateException.
close in interface ObjectPool<T>close in class BaseObjectPool<T>public PooledObjectFactory<T> getFactory()
PooledObjectFactory used by this pool to create and
manage object instances.private void pruneClearedReferences()
Reference wrappers from the idle object pool.private PooledSoftReference<T> findReference(T obj)
obj - returning objectprivate void destroy(PooledSoftReference<T> toDestroy) throws java.lang.Exception
PooledSoftReference and remove it from the idle and all
references pools.toDestroy - PooledSoftReference to destroyjava.lang.Exception - If an error occurs while trying to destroy the objectprivate void removeClearedReferences(java.util.Iterator<PooledSoftReference<T>> iterator)
iterator - iterator over idle/allReferences