0

I'm using a FixedThreadPool for worker threads. Here's how I created it:

ThreadPoolExecutor producerThreadPool = (ThreadPoolExecutor) Executors.newFixedThreadPool(25);

So I set the size of it as 25, I assume there should only be at most 25 active threads at any time? (correct me if I'm wrong).

Then my main thread is constantly trying to submit 30 worker threads at a time if there are idle threads in the thread pool:

if(producerThreadPool.getActiveCount() <= 25)

but when I debug it I got this log:

Mon Jul 23 14:04:42 EDT 2012: {"queued_count":"0","active_count":"28","scheduled_tasks":"28","core_pool_size":"1000","pool_size":"29"}
Mon Jul 23 14:04:43 EDT 2012: {"queued_count":"0","active_count":"3","scheduled_tasks":"33","core_pool_size":"1000","pool_size":"34"}
Mon Jul 23 14:04:44 EDT 2012: {"queued_count":"0","active_count":"0","scheduled_tasks":"60","core_pool_size":"1000","pool_size":"60"}
Mon Jul 23 14:04:45 EDT 2012: {"queued_count":"0","active_count":"0","scheduled_tasks":"90","core_pool_size":"1000","pool_size":"90"}
...

Queue is always empty, core_pool_size is still 1000, and pool_size is keep increasing till 1000, but I assume that core_pool_size should be 25 and there should be queued tasks?

Did I do something wrong or misunderstand this?

Any help would be appreciated, thank you!

This is how I generated the log:

HashMap<String, String> log = new HashMap<String, String>();
log.put("core_pool_size", Integer.toString(producerThreadPool.getCorePoolSize()));
log.put("scheduled_tasks", Long.toString(producerThreadPool.getTaskCount()));
log.put("pool_size", Integer.toString(producerThreadPool.getPoolSize()));
log.put("active_count", Integer.toString(producerThreadPool.getActiveCount()));
log.put("queued_count", Integer.toString(producerThreadPool.getQueue().size()));
String gson_str = gson.toJson(log);
3
  • 1
    how is that log message being generated? Commented Jul 23, 2012 at 18:23
  • 1
    Can you give an example of code which reproduces this? Commented Jul 23, 2012 at 19:10
  • 1
    Could you provide an SSCCE reproducing this, because given the source code of newFixedThreadPool and ThreadPoolExecutor, I fail to see how this is possible. Commented Jul 23, 2012 at 21:00

1 Answer 1

3

According to the documentation ThreadPoolExecutor.getActiveCount() it only gives an approximate count, which is pretty fair considering the overhead required to get an accurate value. So when you see '28' it might not actually be 28 threads concurrently running, but over the duration of the function call it counted 28 threads.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.