Muddassir Rahman
Muddassir Rahman

Reputation: 956

Why Threadpool queue the task instead of reusing the idle threads

Creation of ThreadPool

@PostConstruct
public void postConstruct() {
    threadPool = new ThreadPoolExecutor(corePoolSize != 0 ? corePoolSize : defaultPoolSize, maxPoolSize != 0 ? maxPoolSize : defaultMaxPoolSize, defaultKeepAliveTime, TimeUnit.SECONDS,
            new LinkedBlockingQueue<>(300000));
}

values passed from property file

#ThreadPool
corePool.Size=200
maxPool.Size=400
queue.Capacity=999999
KeepAlive=10

I am submitting the task in below method

  public <T> Future<T> submitCall(Callable<T> callable) {
    logger.info("Submitting , total task {}, active count {} completed count {} queue size {}, threads in pool currently {}, largest thread pool {} ",
            threadPool.getTaskCount(), threadPool.getActiveCount(), threadPool.getCompletedTaskCount(),
            threadPool.getQueue().size(), threadPool.getPoolSize(), threadPool.getLargestPoolSize() );
    return threadPool.submit(callable);
}

logs

2024-11-27 13:46:33,963 [INFO ]---[DAExecutorService.java]---[http-nio-9201-exec-2]: Submitting , total task 200, active count 49 completed count 151 queue size 0, threads in pool currently 200, largest thread pool 200 
2024-11-27 13:46:33,963 [INFO ]---[DAExecutorService.java]---[http-nio-9201-exec-2]: Submitting , total task 201, active count 49 completed count 151 queue size 1, threads in pool currently 200, largest thread pool 200 
2024-11-27 13:46:33,964 [INFO ]---[DAExecutorService.java]---[http-nio-9201-exec-2]: Submitting , total task 202, active count 49 completed count 152 queue size 1, threads in pool currently 200, largest thread pool 200 
2024-11-27 13:46:33,965 [INFO ]---[DAExecutorService.java]---[http-nio-9201-exec-2]: Submitting , total task 203, active count 49 completed count 152 queue size 2, threads in pool currently 200, largest thread pool 200 
2024-11-27 13:46:33,966 [INFO ]---[DAExecutorService.java]---[http-nio-9201-exec-2]: Submitting , total task 204, active count 50 completed count 152 queue size 2, threads in pool currently 200, largest thread pool 200 
2024-11-27 13:46:33,967 [INFO ]---[DAExecutorService.java]---[http-nio-9201-exec-2]: Submitting , total task 205, active count 50 completed count 152 queue size 3, threads in pool currently 200, largest thread pool 200 

As we know, a new thread is created for each task until the core pool size is reached. However, once the core pool size is reached, the logs indicate that the completed task count is 151, and the active task count is 49. The idle threads can be calculated as pool size - active threads, which in this case is 200 - 49. Given that there are idle threads available, why aren't they being reused to handle the new tasks, instead of queuing them?

Upvotes: 0

Views: 41

Answers (0)

Related Questions