Class PoolUtils.SynchronizedObjectPool<T>
- Type Parameters:
T- type of objects in the pool
- All Implemented Interfaces:
Closeable, AutoCloseable, ObjectPool<T>
- Enclosing class:
PoolUtils
Note: This should not be used on pool implementations that already
provide proper synchronization such as the pools provided in the Commons
Pool library. Wrapping a pool that waits for poolable
objects to be returned before allowing another one to be borrowed with
another layer of synchronization will cause liveliness issues or a
deadlock.
- Since:
- 2.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final ObjectPool<T> the underlying object poolprivate final ReentrantReadWriteLockObject whose monitor is used to synchronize methods on the wrapped pool. -
Constructor Summary
ConstructorsConstructorDescriptionSynchronizedObjectPool(ObjectPool<T> pool) Creates a new SynchronizedObjectPool wrapping the given pool. -
Method Summary
Modifier and TypeMethodDescriptionvoidCreates an object using thefactoryor other implementation dependent mechanism, passivate it, and then place it in the idle object pool.Obtains an instance from this pool.voidclear()Clears any objects sitting idle in the pool, releasing any associated resources (optional operation).voidclose()Closes this pool, and free any resources associated with it.intReturns the number of instances currently borrowed from this pool.intReturns the number of instances currently idle in this pool.voidinvalidateObject(T obj) Invalidates an object from the pool.voidreturnObject(T obj) Returns an instance to the pool.toString()Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface ObjectPool
addObjects
-
Field Details
-
readWriteLock
Object whose monitor is used to synchronize methods on the wrapped pool. -
pool
the underlying object pool
-
-
Constructor Details
-
SynchronizedObjectPool
SynchronizedObjectPool(ObjectPool<T> pool) throws IllegalArgumentException Creates a new SynchronizedObjectPool wrapping the given pool.- Parameters:
pool- the ObjectPool to be "wrapped" in a synchronized ObjectPool.- Throws:
IllegalArgumentException- if the pool is null
-
-
Method Details
-
borrowObject
Obtains an instance from this pool.Instances returned from this method will have been either newly created with
PooledObjectFactory.makeObject()or will be a previously idle object and have been activated withPooledObjectFactory.activateObject(PooledObject)and then validated withPooledObjectFactory.validateObject(PooledObject).By contract, clients must return the borrowed instance using
ObjectPool.returnObject(T),ObjectPool.invalidateObject(T), or a related method as defined in an implementation or sub-interface.The behaviour of this method when the pool has been exhausted is not strictly specified (although it may be specified by implementations).
- Specified by:
borrowObjectin interfaceObjectPool<T>- Returns:
- an instance from this pool.
- Throws:
Exception- whenPooledObjectFactory.makeObject()throws an exception.NoSuchElementException- when the pool is exhausted and cannot or will not return another instance.IllegalStateException- afterclosehas been called on this pool.
-
returnObject
Returns an instance to the pool. By contract,objmust have been obtained usingObjectPool.borrowObject()or a related method as defined in an implementation or sub-interface.- Specified by:
returnObjectin interfaceObjectPool<T>- Parameters:
obj- aborrowedinstance to be returned.
-
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>- Parameters:
obj- aborrowedinstance to be disposed.
-
addObject
Creates an object using thefactoryor other implementation dependent mechanism, passivate it, and then place it in the idle object pool.addObjectis useful for "pre-loading" a pool with idle objects. (Optional operation).- Specified by:
addObjectin interfaceObjectPool<T>- Throws:
Exception- whenPooledObjectFactory.makeObject()fails.IllegalStateException- afterObjectPool.close()has been called on this pool.UnsupportedOperationException- when this pool cannot add new idle objects.
-
getNumIdle
public int getNumIdle()Returns the number of instances currently idle in this pool. This may be considered an approximation of the number of objects that can beborrowedwithout creating any new instances. Returns a negative value if this information is not available.- Specified by:
getNumIdlein interfaceObjectPool<T>- Returns:
- the number of instances currently idle in this pool.
-
getNumActive
public int getNumActive()Returns the number of instances currently borrowed from this pool. Returns a negative value if this information is not available.- Specified by:
getNumActivein interfaceObjectPool<T>- Returns:
- the number of instances currently borrowed from this pool.
-
clear
Clears any objects sitting idle in the pool, releasing any associated resources (optional operation). Idle objects cleared must bePooledObjectFactory.destroyObject(PooledObject).- Specified by:
clearin interfaceObjectPool<T>- Throws:
Exception- if the pool cannot be clearedUnsupportedOperationException- if this implementation does not support the operation
-
close
public void close()Closes this pool, and free any resources associated with it.Calling
ObjectPool.addObject()orObjectPool.borrowObject()after invoking this method on a pool will cause them to throw anIllegalStateException.Implementations should silently fail if not all resources can be freed.
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceObjectPool<T>
-
toString
-