|
UC Berkeley Group for User Interface Research Updated November 17, 2000 |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--edu.berkeley.guir.lib.collection.ObjectPool
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
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 |
public ObjectPool()
public ObjectPool(int newCapacity)
capacity
objects.Method Detail |
public static ObjectPool synchronizedObjectPool(ObjectPool p)
public static ObjectPool allocatingObjectPool(ObjectPool p)
public void printDebugStacks()
protected abstract Object createObject()
public int getAvailable()
public int getCapacity()
public Object getObject() throws EmptyException
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).
EmptyException
- (a RuntimeException) if empty.public void releaseObject(Object obj)
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
obj
- is the Object to release back into the pool.RuntimeException
- if we don't own the object. Useful for
detecting errors.public boolean isTaken(Object obj)
public boolean isFree(Object obj)
public boolean ownsObject(Object obj)
public void reset()
public void reinitialize()
public String toString()
toString
in class Object
public static void main(String[] argv)
|
Copyright Information | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |