public class ObjectPool
extends java.lang.Object
The default starting size is 64 objects, and the growth factor is 2.
The pool will be initially filled upon the first invocation of the getObject method, and will automatically grow in the event that getObject finds the pool empty. To initially fill the pool in advance, use the initialize method. Subsequent calls to initialize have no effect.
Objects are obtained from the pool by invoking the getObject method and casting the result to the underlying type. When the object is no longer needed, it may be returned to the pool by invoking the releaseObject method.
To pool a type of object whose constructor takes arguments or whose instances require additional initialization after construction, extend ObjectPool and override the newObject method.
| Constructor and Description |
|---|
ObjectPool(java.lang.Class type)
Create a new
ObjectPool, with the default starting size, to pool objects of the specified type. |
ObjectPool(int startSize, java.lang.Class type)
Create a new
ObjectPool, with the specified starting size, to pool objects of the specified type. |
| Modifier and Type | Method and Description |
|---|---|
int |
available()
Returns the number of pooled objects that are not currently in use.
|
java.lang.Object |
getObject()
Returns an object from the pool.
|
java.lang.Class |
getObjectType()
Returns the
Class for the pooled objects. |
void |
initialize()
Initially fills the pool with the starting number of objects.
|
protected java.lang.Object |
newObject()
Subclasses of
ObjectPool should override this method in order to pool objects whose constructors require arguments or that require post-construction initialization. |
void |
releaseObject(java.lang.Object obj)
Returns the given object to the pool.
|
int |
size()
Returns the total size of the pool, including objects that are currently in use.
|
public ObjectPool(java.lang.Class type)
ObjectPool, with the default starting size, to pool objects of the specified type.
The pool is not filled upon creation. The first call to the getObject method will result in the pool being filled. Alternatively, call the initialize method to fill the pool with constructed objects.
type - The Class of the objects to be pooled.getObject(), initialize()
public ObjectPool(int startSize,
java.lang.Class type)
ObjectPool, with the specified starting size, to pool objects of the specified type.
The pool is not filled upon creation. The first call to the getObject method will result in the pool being filled. Alternatively, call the initialize method to fill the pool with constructed objects.
startSize - The initial number of objects to fill the pool.type - The Class of the objects to be pooled.getObject(), initialize()public void initialize()
getObject has been called also has no effect.getObject()public java.lang.Object getObject()
initialize method have no effect.initialize()public void releaseObject(java.lang.Object obj)
getObject method has no effect.obj - The object to be returned to the pool.public int size()
available()public int available()
sizepublic java.lang.Class getObjectType()
Class for the pooled objects.protected java.lang.Object newObject()
ObjectPool should override this method in order to pool objects whose constructors require arguments or that require post-construction initialization.