UC Berkeley Group for User Interface Research
Updated November 17, 2000

edu.berkeley.guir.lib.collection
Class ObjectPool

java.lang.Object
  |
  +--edu.berkeley.guir.lib.collection.ObjectPool
Direct Known Subclasses:
SatinConstants.ObjectPoolAffineTransform, SatinConstants.ObjectPoolPoint2D, SatinConstants.ObjectPoolPolygon2D, SatinConstants.ObjectPoolRectangle2D, SatinConstants.ObjectPoolStringBuffer

public abstract class ObjectPool
extends Object

An ObjectPool is a way of reusing objects, in order to avoid garbage collection. Essentially, the ObjectPool gives you an instance of the class you need when requested. When you are done, just notify the ObjectPool that you are no longer using this object, and it will be returned to the ObjectPool.

This implementation is not thread-safe. Use synchronizedObjectPool(ObjectPool) for this purpose. instead.

To use this ObjectPool implementation, you just need to subclass and override the createObject() method to create an empty version of the object you want.

Note: I'm having doubts about the effectiveness of using the ObjectPool in newer versions of JDK. I ran some regression tests with and without ObjectPool, and in most of the cases not using ObjectPool actually won. It may be just this implementation, or it might be that HotSpot really is winning out.

This software is distributed under the Berkeley Software License.

 Revisions:  - GUIRLib-v1.0-1.0.0, Aug 18 1999, JH
               Created class
             - GUIRLib-v1.2-1.0.0, Jun 22 2000, JH
               Touched for GUIRLib release
             - GUIRLib-v1.3-1.0.0, Aug 11 2000, JH
               Touched for GUIRLib release
             - GUIRLib-v1.4-1.0.0, Aug 31 2000, JH
               Touched for GUIRLib release
 

Since:
JDK 1.2
Version:
GUIRLib-v1.4-1.0.0, Aug 31 2000
Author:
(jasonh@cs.berkeley.edu)

Constructor Summary
ObjectPool()
          Creates a pool of objects.
ObjectPool(int newCapacity)
          Creates a pool of size capacity objects.
 
Method Summary
static ObjectPool allocatingObjectPool(ObjectPool p)
          Get an object pool that isn't really an object pool, but allocates new objects.
protected abstract  Object createObject()
          Override this method so that it creates the Object you want in the pool.
 int getAvailable()
          Get the number of available objects.
 int getCapacity()
          Get the total capacity of the pool.
 Object getObject()
          Get an object.
 boolean isFree(Object obj)
          See if an object is free.
 boolean isTaken(Object obj)
          See if an object is taken.
static void main(String[] argv)
           
 boolean ownsObject(Object obj)
           
 void printDebugStacks()
          Print what method requested what object from the pool.
 void reinitialize()
          Clear out all of the objects in the pool and create new ones.
 void releaseObject(Object obj)
          Mark this object as released.
 void reset()
          Reset each element in the pool as available.
static ObjectPool synchronizedObjectPool(ObjectPool p)
          Get a synchronized object pool.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ObjectPool

public ObjectPool()
Creates a pool of objects.

ObjectPool

public ObjectPool(int newCapacity)
Creates a pool of size capacity objects.
Method Detail

synchronizedObjectPool

public static ObjectPool synchronizedObjectPool(ObjectPool p)
Get a synchronized object pool.

allocatingObjectPool

public static ObjectPool allocatingObjectPool(ObjectPool p)
Get an object pool that isn't really an object pool, but allocates new objects. Useful for debugging purposes.

printDebugStacks

public void printDebugStacks()
Print what method requested what object from the pool. Must compile with debug flag in order for this to work.

createObject

protected abstract Object createObject()
Override this method so that it creates the Object you want in the pool.

getAvailable

public int getAvailable()
Get the number of available objects.

getCapacity

public int getCapacity()
Get the total capacity of the pool.

getObject

public Object getObject()
                 throws EmptyException
Get an object.

Usage Notes:

Also, be sure not to assign the reference that you get!!! If you do, then you cannot release the object (and will get memory leaks).

Throws:
EmptyException - (a RuntimeException) if empty.

releaseObject

public void releaseObject(Object obj)
Mark this object as released. If this object does not belong to the pool, then ignore.

Override this method to clear out the object's values first, if you want. Be sure to call super correctly. For example, if you have an Object Pool of LinkedList objects, then in your subclass: public void releaseObject(Object obj) { LinkedList list = (LinkedList) obj; list.clear(); super.releaseObject(obj); } // of releaseObject

Parameters:
obj - is the Object to release back into the pool.
Throws:
RuntimeException - if we don't own the object. Useful for detecting errors.

isTaken

public boolean isTaken(Object obj)
See if an object is taken.
Returns:
true if the object is taken, false if not or if it does not belong to this pool.

isFree

public boolean isFree(Object obj)
See if an object is free.
Returns:
true if the object is free, false if not or if it does not belong to this pool.

ownsObject

public boolean ownsObject(Object obj)

reset

public void reset()
Reset each element in the pool as available.

reinitialize

public void reinitialize()
Clear out all of the objects in the pool and create new ones.

toString

public String toString()
Overrides:
toString in class Object

main

public static void main(String[] argv)

Copyright Information