com.ibm.websphere.asynchbeans.pool
Interface ObjectPool

All Known Subinterfaces:
CustomObjectPool

public interface ObjectPool

This is a pool of reusuable objects of the same type. All Object pools implement this type, even custom ones. It can be obtained by calling the ObjectPoolManager.getPool method. This will reuse an existing pool if one already existed, otherwise, a new pool is created. ObjectPools returned by getPool are shared in a JVM.

The ObjectPoolManager.createFastPool method also returns ObjectPools. These are unsynchronized pools that offer a very high level of performance but they are not thread safe.

Here is an example of its usage, first get an ObjectPoolManager instance. See the java docs for that type for an example of how to do that.


 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 try finally blocks to ensure objects obtained from a pool are returned. If an object is now returned then this is a programming error but not a fatal one as Java wil GC the object.

See Also:
ObjectPool

Method Summary
 java.lang.Object getObject()
          This returns an object from the pool.
 void returnObject(java.lang.Object o)
          This should be used to return an object to the pool.
 

Method Detail

getObject

public java.lang.Object getObject()
This returns an object from the pool. If the pool is empty then we create a new instance.

Returns:
An object of the required type.

returnObject

public 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.