For a project I had a look at the apache commons-pool project.. Pooling consists of two parts that enable you to separate the creation and destroying of objects and the pooling of objects:
1. An object pool – Obtains objects from the pool and returns objects to the pool.
2. An Object factory – This means it creates new objects, destroys objects, checks the status of objects, etc.

The commons-pool project provides two sets of interfaces for these two parts, each containing a standard interface and an interface with a key:

public interface ObjectPool {
  Object borrowObject();
  void returnObject(Object borrowed);

public interface KeyedObjectPool {
   Object borrowObject(Object key);
  void returnObject(Object key, Object borrowed);

public interface PoolableObjectFactory {
  Object makeObject();
  void activateObject(Object obj);
  void passivateObject(Object obj);
  boolean validateObject(Object obj);
  void destroyObject(Object obj);

public interface KeyedPoolableObjectFactory {
  Object makeObject(Object key);
  void activateObject(Object key, Object obj);
  void passivateObject(Object key, Object obj);
  boolean validateObject(Object key, Object obj);
  void destroyObject(Object key, Object obj);

The framework also provides some standard implementations for these interfaces. These are the base implementations, all abstract classes with minimal implementation code:

These are all abstract classes that you should extend when writing your own ObjectPools and PoolableObjectFactories. There are also a number of these implementations available. They all extend the base editions.

  • GenericObjectPool : ObjectPool implementation with a FIFO (First In First Out) behavior. This queue like behavior makes sure each object is regularly used. (helps preventing time-outs)
  • StackObjectPool : ObjectPool implementation with a LIFO (Last In First Out) behavior.
  • SoftReferenceObjectPool : ObjectPool implementation with a LIFO (Last In First Out) behavior. Additionally this pool wraps each object in a SoftReference allowing the garbage collector to remove them in response to memory demand.
  • GenericKeyedObjectPool : ObjectPool implementation with a FIFO (First In First Out) behavior.
  • StackKeyedObjectPool : ObjectPool implementation with a LIFO (Last In First Out) behavior.

Now lets have a look at these implementations, first the BaseObjectPool and the GenericObjectPool. The base is an abstract class and gives a default implementation for a few methods. The GenericObjectPool extends from the BaseObjectPool and is a class that can best be extended for your own object pool. The BaseObjectPool needs a PooledObjectFactory to function, this factory is used to obtain new objects, destroy objects, validate objects and some other actions. The GenericObjectPool also provides a Config subclass that helps to set the available configurations parameters. Have a look at the excellent java doc for detailed description of methods and fields. GenericObjectPool and

Time for a very easy example that shows the most basic use of the pooling mechanism:
We need a factory : SamplePoolableObjectFactory implements PoolableObjectFactory
and an object pool : SampleObjectPool extends GenericObjectPool

The only thing that the different methods do is logging, oke except for the makeObject and destroyObject. The make creates a new object and the destroy sets it to null.

Then we make a runnable application that obtains and returns a number of objects from the pool.

public class RunApp {

  public static void main(String args) throws Exception {
    System.out.println(“Starting app”);
    SamplePoolableObjectFactory factory = new SamplePoolableObjectFactory();
    SampleObjectPool pool = new SampleObjectPool(factory);

    Object obj1 = pool.borrowObject();
    Object obj2 = pool.borrowObject();


    Object obj3 = pool.borrowObject();

    System.out.println(“Closing app”);

From the code you can see we obtain two objects from the pool, then we return one of the objects and get another one. We expect that the third object is the same as the first. By the way, the numbers in the log messages are hashcodes. The result is:

Starting app
New pool created with factory …
Borrow an object …
Make object …24807699
Activate object …24807699
Borrow an object …
Make object …19608436
Activate object …19608436
Return an object …
Passivate object …24807699
Borrow an object …
Activate object …24807699
Return an object …
Passivate object …19608436
Return an object …
Passivate object …24807699
Closing app

This is as expected.

Hope this helps creating your own Object Pool implementation. You can download the eclipse project here

Using apache commons-pool project