Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7901071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:07:39+00:00 2026-06-03T09:07:39+00:00

I’m trying to pool some objects and share them but I noticed blocking in

  • 0

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.
*/
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T09:07:40+00:00Added an answer on June 3, 2026 at 9:07 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.