Search code examples
javamultithreadingconcurrencythreadpoolthreadpoolexecutor

What does "active threads" mean in ThreadPoolExecutor?


I have ThreadPoolExecutor whith next params:

corePoolSize = 20
maximumPoolSize = 100
workQueue = new LinkedBlockingQueue(10)

Each second I submit 1k tasks. Tasks represent some IO operation which takes 1-2s. Some of them get rejected. It's expected. I log the thread pool state before rejection using custom AbortPolicy.

In logs I see something I cannot explain. Messages like:

java.util.concurrent.ThreadPoolExecutor@3e6be2d8[Running, pool size = 100, active threads = 2, queued tasks = 10, completed tasks = 4471827].

I've found in docs that getActiveCount method Returns the approximate number of threads that are actively executing tasks. But it does not help me to understand...

What does "active threads" mean here? And why only 2 threads are active while pool has reached its limit, queue is full and the task is rejected?

I want to increase maximumPoolSize to process more tasks at once but firstly I need to understand whether I use ThreadPool resources efficiently.


Solution

  • "Active thread" means a thread that has started executing a task from the work queue.

    When threads are started or become free, they don't pick up a task "immediately" - there's contention to read from the queue. If you submit 100 tasks to the executor in a tight loop, it's likely that by the end only a few worker threads have had the chance to pick up a task.

    To get a better picture of how many threads are actually running tasks, log the active thread count on a regular schedule, for example 10 times per second.