SSF.OS
Class ObjectPool
java.lang.Object
|
+--SSF.OS.ObjectPool
- public abstract class ObjectPool
- extends java.lang.Object
Generic base class for pools of objects. By using an object pool,
an alert modeler may be able to avoid calling new() to create new
objects. This can save significant amounts of time, especially in
parallel code, since new() has the potential to create lock contention
for the heap.
To create a pool of objects of class Foo, one might use an anonymous
inner class:
ObjectPool fooPool = new ObjectPool() {
public Object makeObject() { return new Foo(); };
};
Then you would replace calls to new Foo()
by calls to
fooPool.obtain()
. If the pool is empty, obtain
simply calls new()
, so little time is wasted.
To get the benefits of using a pool, you must also try to
return instances of Foo to the pool after their last use, by calling
fooPool.recycle(deadFoo)
. Remember to 'clean up' each
object before you recycle it, by removing all references to it from
elsewhere in your data.
If you forget to recycle an object, it's okay: the Java garbage
collector will eventually do so for you.
If you specify a pool that's too large, and don't make use of the objects
awaiting recycling, you may increase memory consumption by preventing
the Java garbage collector from doing its job. Try to choose a max
pool size that reflects the typical imbalance at any point between
recyclers and consumers of Objects.
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
DEFAULT_MIN_POOL_SIZE
public static final int DEFAULT_MIN_POOL_SIZE
- See Also:
- Constant Field Values
DEFAULT_MAX_POOL_SIZE
public static final int DEFAULT_MAX_POOL_SIZE
- See Also:
- Constant Field Values
ObjectPool
public ObjectPool()
ObjectPool
public ObjectPool(int min,
int max)
makeObject
public abstract java.lang.Object makeObject()
obtain
public java.lang.Object obtain()
recycle
public void recycle(java.lang.Object obj)