Search code examples
javaconnection-poolingpool

Can I increase the timeout for apache pool to reduce creation of new objects?


I'm trying to pool some objects and share them but I noticed blocking in the threads. I'm a bit new to Java so not sure if this is a problem with my lack of experience or something specific to pools. Here's some code that replicates the problem(Create 10 threads and share 20 objects, do this in a long loop so you can catch the blocking). If you profile it(Java visualvm or yourkit in the thread view), you'll notice that borrowObject seems to be blocking the thread. So the question is, is this normal behavior or am I doing something wrong?

I could be completely wrong but I read somewhere that it maybe blocking when it creates/destorys new objects for the pool. I just don't understand how to increase the lifespan of the objects and why it would even need to destory/make if the threads are not idle?

Here's an example:

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.apache.commons.pool.BasePoolableObjectFactory;
import org.apache.commons.pool.ObjectPool;
import org.apache.commons.pool.impl.GenericObjectPool;

public class main{
    public static void main(String[] a){
        ObjectPool<Foo> fpool = new GenericObjectPool<Foo>(new FooPoolableObjectFactory(), 20);
        ExecutorService tpool = Executors.newFixedThreadPool(10);

        for(int i = 0; i < 900000000; ++i){
            tpool.submit(new FooWorker(fpool));
        }
    }
}

class Foo{
    private static int pk = 0;
    private int count = 0;
    public final int id;

    public Foo(){
        id = pk++;
    }

    public int increment(){
        return ++count;
    }
}

class FooWorker implements Runnable{
    private ObjectPool<Foo> fpool;

    public FooWorker(ObjectPool<Foo> fpool){
        this.fpool = fpool;
    }

    @Override
    public void run(){
        Foo foo = null;
        try{
            foo = fpool.borrowObject();
            //System.out.println(foo.id + ": " + foo.increment());
        }
        catch(Exception e){
            e.printStackTrace();
        }
        finally{
            // This is done in a finally block to ensure the object is returned to the pool
            if(foo != null){
                try{
                    fpool.returnObject(foo);
                }
                catch(Exception e){
                    e.printStackTrace();
                }
            }
        }
    }
}

class FooPoolableObjectFactory extends BasePoolableObjectFactory<Foo>{

    @Override
    public Foo makeObject() throws Exception{
        return new Foo();
    }
}

/*
Example output:
1: 1
0: 1
2: 1
3: 1
2: 2
0: 2
2: 3
0: 3
1: 2
3: 2

The number on the left is the id of the foo object being used.
The number on the right is the value of the foo object.

Notice how the foos are being reused.
*/

Solution

  • not sure what you mean by "blocking" the thread (multiple threads waiting on the same mutex?). the apache pool implementation uses simple synchronization, so it is not the most concurrent solution if your tasks are dominated by the time it takes to add and remove objects from the pool. assuming that you did some real work between the borrow and return calls, you would probably see less blocking.