[prev in list] [next in list] [prev in thread] [next in thread] 

List:       jakarta-commons-dev
Subject:    [jira] [Closed] (POOL-218) Does borrowObject block?
From:       "Phil Steitz (JIRA)" <jira () apache ! org>
Date:       2014-12-31 16:52:13
Message-ID: JIRA.12553862.1336096871000.122366.1420044733530 () Atlassian ! JIRA
[Download RAW message or body]


     [ https://issues.apache.org/jira/browse/POOL-218?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel \
]

Phil Steitz closed POOL-218.
----------------------------

> Does borrowObject block?
> ------------------------
> 
> Key: POOL-218
> URL: https://issues.apache.org/jira/browse/POOL-218
> Project: Commons Pool
> Issue Type: Bug
> Affects Versions: 1.6
> Environment: Mac lion and java 1.6
> Reporter: Gautam
> Priority: Trivial
> 
> 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, 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? {code}
> 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();
> }
> }
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic