DESY ACOP Beans Home

com.cosylab.util
Class ObjectPool

java.lang.Object
  extended by com.cosylab.util.ObjectPool

public class ObjectPool
extends java.lang.Object

A simple object pool. This pool is created with an initial capacity and a class (must be a subclass of PoolableObject) of the objects it contains. The pool contains at most capacity of resurrected objects, which it clears (by invoking PoolableObject.clear) before returning them to the user. The user can request a new instance. If found in the pool, it is cleared and returned. Otherwise a new instance creation is attempted with the default constructor. If it fails, null is returned.


Constructor Summary
ObjectPool(java.lang.Class objectType, int capacity)
          Creates a new object pool with an initial capacity and the type of the pooled objects.
ObjectPool(ObjectFactory objectFactory, int capacity)
          Creates a new object pool with an initial capacity and ObjectFactory for creating new instances of pooled objects.
 
Method Summary
static void main(java.lang.String[] args)
          Contains a test sample implementation of a poolable object and the use of thread pool.
 PoolableObject newInstance()
          Produces a new instance.
 void returnToPool(PoolableObject p)
          Must be invoked from the finalize method of the PoolableObjects to return them to the object pool.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ObjectPool

public ObjectPool(java.lang.Class objectType,
                  int capacity)
Creates a new object pool with an initial capacity and the type of the pooled objects. ObjectPool will try to use DefaultObjecFactory with the type of the pooled objects.

Parameters:
objectType - a type descending from PoolableObject
capacity - the initial capacity of the object pool

ObjectPool

public ObjectPool(ObjectFactory objectFactory,
                  int capacity)
Creates a new object pool with an initial capacity and ObjectFactory for creating new instances of pooled objects.

Parameters:
objectFactory - ObjectFactory for creating new instances of pooled objects
capacity - the initial capacity of the object pool
Method Detail

newInstance

public PoolableObject newInstance()
Produces a new instance. Returns an object from the pool, if it is available. Otherwise tries to create new instance with ObjectFactory

Returns:
PoolableObject a new cleared object ready for use or null if the new type construction failed

returnToPool

public void returnToPool(PoolableObject p)
Must be invoked from the finalize method of the PoolableObjects to return them to the object pool. This method will add the object to the pool if there is any room left, otherwise the reference and the object will be discarded.

Parameters:
p - the object being returned to the pool

main

public static void main(java.lang.String[] args)
Contains a test sample implementation of a poolable object and the use of thread pool.

Parameters:
args - ignored.


Copyright © 2010. All Rights Reserved.