In this article, we mainly discuss how to implement an object pool in Java. In recent years, the performance of Java virtual machines has been greatly improved in all aspects, so it is no longer necessary to improve performance through object pools for most objects. The fundamental reason is that the overhead of creating a new object is not as expensive as it used to be.
However, there are still some objects whose creation overhead is very high, such as threads. data base Connections and other non-lightweight objects. In any application, we will definitely use more than one such object. If there is a convenient way to create a pool to manage these objects, so that these objects can be reused dynamically, and the client code does not care about their lifecycle, it will still be very powerful.
Before we really start coding, let's sort out what the next object pool needs to do.
- If there are objects available, the object pool should be able to return to the client.
- After the client puts the objects back into the pool, they can be reused.
- Object pools can create new objects to meet the growing needs of clients.
- A proper pool closure mechanism is needed to ensure that no memory leaks occur after closure.
Needless to say, the above points are the basic functions of the interface we want to expose to the client's connection pool.
The interface of our statement is as follows:
package com.test.pool;
/**
* Represents a cached pool of objects.
*
* @author Swaranga
*
* @param <T> the type of object to pool.
*/
public interface Pool<T>
{
/**
* Returns an instance from the pool.
* The call may be a blocking one or a non-blocking one
* and that is determined by the internal implementation.
*
* If the call is a blocking call,
* the call returns immediately with a valid object
* if available, else the thread is made to wait
* until an object becomes available.
* In case of a blocking call,
* it is advised that clients react
* to {@link InterruptedException} which might be thrown
* when the thread waits for an object to become available.
*
* If the call is a non-blocking one,
* the call returns immediately irrespective of
* whether an object is available or not.
* If any object is available the call returns it
* else the call returns < code >null< /code >.
*
* The validity of the objects are determined using the
* {@link Validator} interface, such that
* an object < code >o< /code > is valid if
* < code > Validator.isValid(o) == true < /code >.
*
* @return T one of the pooled objects.
*/
T get();
/**
* Releases the object and puts it back to the pool.
*
* The mechanism of putting the object back to the pool is
* generally asynchronous,
* however future implementations might differ.
*
* @param t the object to return to the pool
*/
void release(T t);
/**
* Shuts down the pool. In essence this call will not
* accept any more requests
* and will release all resources.
* Releasing resources are done
* via the < code >invalidate()< /code >
* method of the {@link Validator} interface.
*/
void shutdown();
}
In order to support any object, the above interface is deliberately designed to be simple and general. It provides methods to retrieve/return objects from the pool, and a mechanism to close the pool to release objects.
Now let's implement this interface. Before starting, it is worth mentioning that an ideal release method should first try to check whether the object returned by the client can be reused. If so, throw it back into the pool. If not, discard the object. We hope that all implementations of this Pool interface will follow this rule. Before we start with concrete implementation classes, we first create an abstract class to restrict subsequent implementations from following this. The abstract class we implement is called AbstractPool, which is defined as follows:
package com.test.pool;
/**
* Represents an abstract pool, that defines the procedure
* of returning an object to the pool.
*
* @author Swaranga
*
* @param <T> the type of pooled objects.
*/
abstract class AbstractPool <T> implements Pool <T>
{
/**
* Returns the object to the pool.
* The method first validates the object if it is
* re-usable and then puts returns it to the pool.
*
* If the object validation fails,
* some implementations
* will try to create a new one
* and put it into the pool; however
* this behaviour is subject to change
* from implementation to implementation
*
*/
@Override
public final void release(T t)
{
if(isValid(t))
{
returnToPool(t);
}
else
{
handleInvalidReturn(t);
}
}
protected abstract void handleInvalidReturn(T t);
protected abstract void returnToPool(T t);
protected abstract boolean isValid(T t);
}
In the above class, we let the object pool have to validate the object before putting it back into the pool. Specific implementations are free to choose how to implement these three methods in order to customize their own behavior. They use their own logic to determine how an object is valid, how it should be handled if it is invalid (handleInvalidReturn method), and how to put an effective object back into the pool (returnToPool method).
With these classes, we can start to implement them in detail. However, there is another problem, because the above classes are designed to support common object pools, the specific implementation does not know how to verify the validity of objects (because objects are generic). So we need something else to help us do it.
We need a general method to complete object verification, and the specific implementation does not need to care about the type of object. So we introduce a new interface, Validator, which defines the method of validating objects. This interface is defined as follows:
package com.test.pool;
/**
* Represents the functionality to
* validate an object of the pool
* and to subsequently perform cleanup activities.
*
* @author Swaranga
*
* @param < T > the type of objects to validate and cleanup.
*/
public static interface Validator < T >
{
/**
* Checks whether the object is valid.
*
* @param t the object to check.
*
* @return <code>true</code>
* if the object is valid else <code>false</code>.
*/
public boolean isValid(T t);
/**
* Performs any cleanup activities
* before discarding the object.
* For example before discarding
* database connection objects,
* the pool will want to close the connections.
* This is done via the
* <code>invalidate()</code> method.
*
* @param t the object to cleanup
*/
public void invalidate(T t);
}
The interface above defines a method of checking objects and a method of invalidating objects. The invalidate method comes in handy when you're ready to discard an object and clean up memory. It is worth noting that the interface itself has no meaning, only when it is used in the object pool, so we define this interface into the Pool interface. This is the same as Map and Map.Entry in the Java Collection Library. So our Pool interface is like this:
package com.test.pool;
/**
* Represents a cached pool of objects.
*
* @author Swaranga
*
* @param < T > the type of object to pool.
*/
public interface Pool< T >
{
/**
* Returns an instance from the pool.
* The call may be a blocking one or a non-blocking one
* and that is determined by the internal implementation.
*
* If the call is a blocking call,
* the call returns immediately with a valid object
* if available, else the thread is made to wait
* until an object becomes available.
* In case of a blocking call,
* it is advised that clients react
* to {@link InterruptedException} which might be thrown
* when the thread waits for an object to become available.
*
* If the call is a non-blocking one,
* the call returns immediately irrespective of
* whether an object is available or not.
* If any object is available the call returns it
* else the call returns < code >null< /code >.
*
* The validity of the objects are determined using the
* {@link Validator} interface, such that
* an object < code >o< /code > is valid if
* < code > Validator.isValid(o) == true < /code >.
*
* @return T one of the pooled objects.
*/
T get();
/**
* Releases the object and puts it back to the pool.
*
* The mechanism of putting the object back to the pool is
* generally asynchronous,
* however future implementations might differ.
*
* @param t the object to return to the pool
*/
void release(T t);
/**
* Shuts down the pool. In essence this call will not
* accept any more requests
* and will release all resources.
* Releasing resources are done
* via the < code >invalidate()< /code >
* method of the {@link Validator} interface.
*/
void shutdown();
/**
* Represents the functionality to
* validate an object of the pool
* and to subsequently perform cleanup activities.
*
* @author Swaranga
*
* @param < T > the type of objects to validate and cleanup.
*/
public static interface Validator < T >
{
/**
* Checks whether the object is valid.
*
* @param t the object to check.
*
* @return <code>true</code>
* if the object is valid else <code>false</code>.
*/
public boolean isValid(T t);
/**
* Performs any cleanup activities
* before discarding the object.
* For example before discarding
* database connection objects,
* the pool will want to close the connections.
* This is done via the
* <code>invalidate()</code> method.
*
* @param t the object to cleanup
*/
public void invalidate(T t);
}
}
Preparations are almost complete, and we need an ultimate weapon before we finally start. That's the killer's weapon of this pool. It's the ability to create new objects. Our object pools are generic, so they know how to generate new objects to fill the pool. This functionality cannot depend on the object pool itself, and there must be a common way to create new objects. This can be done through an ObjectFactory interface, which has only one "how to create new objects" method. Our ObjectFactory interface is as follows:
package com.test.pool;
/**
* Represents the mechanism to create
* new objects to be used in an object pool.
*
* @author Swaranga
*
* @param < T > the type of object to create.
*/
public interface ObjectFactory < T >
{
/**
* Returns a new instance of an object of type T.
*
* @return T an new instance of the object of type T
*/
public abstract T createNew();
}
Our toolkit classes have been completed, and now we can begin to really implement our Pool interface. Because we want this pool to be used in concurrent programs, we will create a blocked pool of objects that will be blocked by the client first when no objects are available. Our blocking mechanism is to keep the client blocking until there are objects available. This leads to an additional method that blocks only a certain amount of time, returns if an object is available before the time-out arrives, and returns null if the time-out occurs instead of waiting. This implementation is somewhat similar to LinkedBlockingQueue in Java concurrent libraries, so before we really implement it, we expose another interface, BlockingPool, which is similar to the BlockingQueue interface in Java concurrent libraries.
Here is the statement of BlockingQueue:
package com.test.pool;
import java.util.concurrent.TimeUnit;
/**
* Represents a pool of objects that makes the
* requesting threads wait if no object is available.
*
* @author Swaranga
*
* @param < T > the type of objects to pool.
*/
public interface BlockingPool < T > extends Pool < T >
{
/**
* Returns an instance of type T from the pool.
*
* The call is a blocking call,
* and client threads are made to wait
* indefinitely until an object is available.
* The call implements a fairness algorithm
* that ensures that a FCFS service is implemented.
*
* Clients are advised to react to InterruptedException.
* If the thread is interrupted while waiting
* for an object to become available,
* the current implementations
* sets the interrupted state of the thread
* to <code>true</code> and returns null.
* However this is subject to change
* from implementation to implementation.
*
* @return T an instance of the Object
* of type T from the pool.
*/
T get();
/**
* Returns an instance of type T from the pool,
* waiting up to the
* specified wait time if necessary
* for an object to become available..
*
* The call is a blocking call,
* and client threads are made to wait
* for time until an object is available
* or until the timeout occurs.
* The call implements a fairness algorithm
* that ensures that a FCFS service is implemented.
*
* Clients are advised to react to InterruptedException.
* If the thread is interrupted while waiting
* for an object to become available,
* the current implementations
* set the interrupted state of the thread
* to <code>true</code> and returns null.
* However this is subject to change
* from implementation to implementation.
*
*
* @param time amount of time to wait before giving up,
* in units of <tt>unit</tt>
* @param unit a <tt>TimeUnit</tt> determining
* how to interpret the
* <tt>timeout</tt> parameter
*
* @return T an instance of the Object
* of type T from the pool.
*
* @throws InterruptedException
* if interrupted while waiting
*/
T get(long time, TimeUnit unit) throws InterruptedException;
}
The implementation of Bounded Blocking Pool is as follows:
package com.test.pool;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
public final class BoundedBlockingPool
extends <AbstractPool>
implements <BlockingPool>
{
private int size;
private BlockingQueue objects;
private Validator validator;
private ObjectFactory objectFactory;
private ExecutorService executor =
Executors.newCachedThreadPool();
private volatile boolean shutdownCalled;
public BoundedBlockingPool(
int size,
Validator validator,
ObjectFactory objectFactory)
{
super();
this.objectFactory = objectFactory;
this.size = size;
this.validator = validator;
objects = new LinkedBlockingQueue (size);
initializeObjects();
shutdownCalled = false;
}
public T get(long timeOut, TimeUnit unit)
{
if(!shutdownCalled)
{
T t = null;
try
{
t = objects.poll(timeOut, unit);
return t;
}
catch(InterruptedException ie)
{
Thread.currentThread().interrupt();
}
return t;
}
throw new IllegalStateException(
'Object pool is already shutdown');
}
public T get()
{
if(!shutdownCalled)
{
T t = null;
try
{
t = objects.take();
}
catch(InterruptedException ie)
{
Thread.currentThread().interrupt();
}
return t;
}
throw new IllegalStateException(
'Object pool is already shutdown');
}
public void shutdown()
{
shutdownCalled = true;
executor.shutdownNow();
clearResources();
}
private void clearResources()
{
for(T t : objects)
{
validator.invalidate(t);
}
}
@Override
protected void returnToPool(T t)
{
if(validator.isValid(t))
{
executor.submit(new ObjectReturner(objects, t));
}
}
@Override
protected void handleInvalidReturn(T t)
{
}
@Override
protected boolean isValid(T t)
{
return validator.isValid(t);
}
private void initializeObjects()
{
for(int i = 0; i < size; i++)
{
objects.add(objectFactory.createNew());
}
}
private class ObjectReturner
implements <Callable>
{
private BlockingQueue queue;
private E e;
public ObjectReturner(BlockingQueue queue, E e)
{
this.queue = queue;
this.e = e;
}
public Void call()
{
while(true)
{
try
{
queue.put(e);
break;
}
catch(InterruptedException ie)
{
Thread.currentThread().interrupt();
}
}
return null;
}
}
}
The above is a very basic object pool, which is internally based on a Linked Blocking Queue. The only interesting way to do this is to return to ToPool. Because the internal storage is implemented by a LinkedBlockingQueue, if we throw the returned object directly, the client may be blocked if the queue is full. But we don't want the client to block because of such a common way of putting objects back into the pool. So we submit the task that eventually inserts the object into the queue as an asynchronous task to one Executor Execute it so that the client thread can return immediately.
Now we will use the object pool above in our code to cache database connections. We need a validator to verify that the database connection is valid.
Here is the JDBC Connection Validator:
package com.test;
import java.sql.Connection;
import java.sql.SQLException;
import com.test.pool.Pool.Validator;
public final class JDBCConnectionValidator implements Validator < Connection >
{
public boolean isValid(Connection con)
{
if(con == null)
{
return false;
}
try
{
return !con.isClosed();
}
catch(SQLException se)
{
return false;
}
}
public void invalidate(Connection con)
{
try
{
con.close();
}
catch(SQLException se)
{
}
}
}
There is also a JDBCObjectFactory, which will be used to generate new database connection objects:
package com.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import com.test.pool.ObjectFactory;
public class JDBCConnectionFactory implements ObjectFactory < Connection >
{
private String connectionURL;
private String userName;
private String password;
public JDBCConnectionFactory(
String driver,
String connectionURL,
String userName,
String password) {
super();
try
{
Class.forName(driver);
}
catch(ClassNotFoundException ce)
{
throw new IllegalArgumentException('Unable to find driver in classpath', ce);
}
this.connectionURL = connectionURL;
this.userName = userName;
this.password = password;
}
public Connection createNew()
{
try
{
return DriverManager.getConnection(
connectionURL,
userName,
password);
}
catch(SQLException se)
{
throw new IllegalArgumentException('Unable to create new connection', se);
}
}
}
Now we use the above Validator and ObjectFactory to create a JDBC connection pool:
package com.test;
import java.sql.Connection;
import com.test.pool.Pool;
import com.test.pool.PoolFactory;
public class Main
{
public static void main(String[] args)
{
Pool < Connection > pool =
new BoundedBlockingPool < Connection > (
10,
new JDBCConnectionValidator(),
new JDBCConnectionFactory('', '', '', '')
);
//do whatever you like
}
}
In order to reward readers who can read the full article, I will provide another implementation of non-blocking object pool. The only difference between this implementation and the previous one is that even if the object is not available, it does not block the client, but returns directly to null. The concrete realization is as follows:
package com.test.pool;
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.Semaphore;
public class BoundedPool < T > extends AbstractPool < T >
{
private int size;
private Queue < T > objects;
private Validator < T > validator;
private ObjectFactory < T > objectFactory;
private Semaphore permits;
private volatile boolean shutdownCalled;
public BoundedPool(
int size,
Validator < T > validator,
ObjectFactory < T > objectFactory)
{
super();
this.objectFactory = objectFactory;
this.size = size;
this.validator = validator;
objects = new LinkedList < T >();
initializeObjects();
shutdownCalled = false;
}
@Override
public T get()
{
T t = null;
if(!shutdownCalled)
{
if(permits.tryAcquire())
{
t = objects.poll();
}
}
else
{
throw new IllegalStateException('Object pool already shutdown');
}
return t;
}
@Override
public void shutdown()
{
shutdownCalled = true;
clearResources();
}
private void clearResources()
{
for(T t : objects)
{
validator.invalidate(t);
}
}
@Override
protected void returnToPool(T t)
{
boolean added = objects.add(t);
if(added)
{
permits.release();
}
}
@Override
protected void handleInvalidReturn(T t)
{
}
@Override
protected boolean isValid(T t)
{
return validator.isValid(t);
}
private void initializeObjects()
{
for(int i = 0; i < size; i++)
{
objects.add(objectFactory.createNew());
}
}
}
Considering that we now have two implementations, it's very powerful to let users create different object pools by factory with specific names. Here comes the factory:
package com.test.pool;
import com.test.pool.Pool.Validator;
/**
* Factory and utility methods for
* {@link Pool} and {@link BlockingPool} classes
* defined in this package.
* This class supports the following kinds of methods:
*
*
<ul>
*
<li> Method that creates and returns a default non-blocking
* implementation of the {@link Pool} interface.
* </li>
*
*
<li> Method that creates and returns a
* default implementation of
* the {@link BlockingPool} interface.
* </li>
*
</ul>
*
* @author Swaranga
*/
public final class PoolFactory
{
private PoolFactory()
{
}
/**
* Creates a and returns a new object pool,
* that is an implementation of the {@link BlockingPool},
* whose size is limited by
* the <tt> size </tt> parameter.
*
* @param size the number of objects in the pool.
* @param factory the factory to create new objects.
* @param validator the validator to
* validate the re-usability of returned objects.
*
* @return a blocking object pool
* bounded by <tt> size </tt>
*/
public static < T > Pool < T >
newBoundedBlockingPool(
int size,
ObjectFactory < T > factory,
Validator < T > validator)
{
return new BoundedBlockingPool < T > (
size,
validator,
factory);
}
/*
* Creates a and returns a new object pool,
* that is an implementation of the {@link Pool}
* whose size is limited
* by the <tt> size </tt> parameter.
*
* @param size the number of objects in the pool.
* @param factory the factory to create new objects.
* @param validator the validator to validate
* the re-usability of returned objects.
*
* @return an object pool bounded by <tt> size </tt>
*/
public static < T > Pool < T > newBoundedNonBlockingPool(
int size,
ObjectFactory < T > factory,
Validator < T > validator)
{
return new BoundedPool < T >(size, validator, factory);
}
}
Now our client can create object pools in a more readable way:
package com.test;
import java.sql.Connection;
import com.test.pool.Pool;
import com.test.pool.PoolFactory;
public class Main
{
public static void main(String[] args)
{
Pool < Connection > pool =
PoolFactory.newBoundedBlockingPool(
10,
new JDBCConnectionFactory('', '', '', ''),
new JDBCConnectionValidator());
//do whatever you like
}
}