Object Pool



This is the object pool.
This class will create and pool conectionImpl objects. 
When a calling class requests a conectionImpl it will
provide one, only creating it if nessesary.
Note: This is a singleton class.
-----------------------------------------------------
class ConnectionPool{

    private static ConnectionPool connectionPool = new ConnectionPool();

    private Vector pool = new Vector();

    //private constructor -singelton
    private ConnectionPool(){}

    public static ConnectionPool getInstance(){
        return connectionPool;
    }

    public synchronized ConnectionImpl acquireImpl(){
        if(pool.size()>0){
            return (ConnectionImpl)pool.remove(pool.size()-1);
        }
        return new ConnectionImpl();
    }

    public void releaseImpl(ConnectionImpl impl){
        pool.addElement(impl);
    }
}
-----------------------------------------------------


This is the connectionImpl class. It is assumend 
creation of these classes is expensive.
-----------------------------------------------------
class ConnectionImpl{
    //yea yea-I am not going to make accessors for this example.
    public boolean inUse = false;
}
-----------------------------------------------------


This is the connection class. It only gets a connectionImpl
when nessesary and when finished releases it to be reused.
-----------------------------------------------------
class Connection{
    
    //private BoundedConnectionPool pool = BoundedConnectionPool.getInstance();
    private ConnectionPool pool = ConnectionPool.getInstance();
  
    private ConnectionImpl connectionImpl;

    public void doIt(){
        connectionImpl = pool.acquireImpl();
        //do stuff
        pool.releaseImpl(connectionImpl);
    }
}
-----------------------------------------------------


This is a test stub to run everything.
-----------------------------------------------------
class Test{

    public static void main(String[] args){
        System.out.println("class: Test, method: main");

        Connection  connection = new Connection();
        connection.doIt();
                
    }
}
-----------------------------------------------------






ALTERNITVE CONNECTION POOL CLASS:

Here is an alternitive object pool that is bounded.
It will only create a bounded number of connectionImpl
objects pooling these and refusing to create more.
If the pool is empty a calling object must try again 
when a connectionImpl has been released. 
The call acquireImpl could block until a connectionImpl
becomes available or use the listner pattern to  
notify connection classes. This example simply returns
null.
-----------------------------------------------------
class BoundedConnectionPool{

    private static BoundedConnectionPool connectionPool = new BoundedConnectionPool();

    private Vector pool = new Vector();

    //private constructor -singelton
    private BoundedConnectionPool(){}

    public static BoundedConnectionPool getInstance(){
        return connectionPool;
    }

    public synchronized ConnectionImpl acquireImpl(){
        ConnectionImpl temp;
        for(int i =0; i<pool.size(); i++){
            temp = (ConnectionImpl)pool.get(i);
            if(!temp.inUse){
                temp.inUse = true;
                return temp;
            }
        }
        if(pool.size()<5){
            temp = new ConnectionImpl();
            temp.inUse = true;
            pool.add(temp);
            return temp;
        }
        return null;
    }

    public void releaseImpl(ConnectionImpl impl){
        ConnectionImpl temp;
        for(int i =0; i<pool.size(); i++){
            temp = (ConnectionImpl)pool.get(i);
            if(temp == impl){
                System.out.println("releasing impl");
                temp.inUse = false;
            }
        }
    }
}
-----------------------------------------------------



Here is a tar with the above java classes in it. 
objectPool.tar