com.ibm.websphere.asynchbeans.pool
Interface ObjectPool
All known subinterfaces:
- public interface ObjectPool
The ObjectPoolManager.createFastPool method also returns object pools. These are unsynchronized pools that offer a very high level of performance, but they are not thread safe.
Here is an example of usage for an object pool. First, get an ObjectPoolManager instance. See the ObjectPoolManager JavaDoc for an example. Then, request an object pool of the desired class. Use the object pool to manage instances of the class as follows.
ObjectPoolManager opm = ...; ObjectPool vectorPool = opm.getPool(Vector.class); Vector pooledVector = null; try { pooledVector = (Vector)vectorPool.getObject(); Now use the vector. } finally { if(pooledVector != null) { vectorPool.returnObject(pooledVector); pooledVector = null; } }
We recommend the use of a
finally
block to ensure objects obtained from a
pool are returned. If an object is not returned then the
memory used by the object can eventually be freed by Java garbage collection.
See Also:
Method Summary
Modifier and Type | Method and Description |
---|---|
|
getObject()
This method obtains an object from the pool.
|
|
returnObject(java.lang.Object o)
This should be used to return an object to the pool.
|
Method Detail
getObject
- java.lang.Object getObject()
This method obtains an object from the pool.
If the pool is empty then we create a new instance.
Returns:
An object of the type defined for the object pool.
returnObject
- void returnObject(java.lang.Object o)
This should be used to return an object to the pool.
Care needs to be taken to return objects of the
correct type only.
Objects that are Collections are automatically cleared when returned to the pool. Collections can only be pooled by the ObjectPool if they support the optional clear method on the Collection interface.
Parameters:
o
- The object to be returned to the pool. This must be the expected type. Throws:
java.lang.UnsupportedOperationException
- if the object being returned is a Collection
that doesn't support the clear method.