On Sat, May 5, 2012 at 12:07 AM, Gautam Bakshi <[email protected]>wrote:
> Hi Everyone, > > 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? Is there any way I can get around it? > > Hi Gautum, What your describing is normal behaviour, after all if you ask the pool for an object and one is not available what else should it do. The best thing is to wait(block) until another thread returns a pooled item and then return with that. The reason for using pooling is typically the pooled objects are expensive or limited resources which is why your code does not simply create another one when it needs it, but rather waits its turn and then returns the pooled object when its used. hth mP
