Previous Topic

Next Topic

Book Contents

Book Index

Object Pool utility

The object pool utility creates pools of reusable objects of certain type. It helps you get free instances of a class and easily release them if you need them no more.

The components of the object pool utility are in the com.prosyst.util.pool package. A pool is represented by an ObjectPool instance.

You should specify the object type to the pool when instantiating ObjectPool. There are two ways to define the type of objects to keep in the pool:

The object pool maintains objects as a two-dimensional array, which is characterized by size and by factor. The size stands for the first dimension of this object array (Object [][]), that is the number of contained one-dimensional arrays. The factor stands for the second dimension of the object array (Object [][]), that is the number of elements contained in a single one-dimensional array. You can also specify the minimum number of objects in the pool which is set to 1 if such is not explicitly defined.

You get an object by calling the getObject method. If there is a free object in the pool, the method returns it to the requester. Otherwise, it returns a new instance. To release an object, invoke the releaseObject method.

If the objects created in the pool become too many, you can shrink their number to the defined minimum by calling the shrink method.