0

I'm struggling with the ThreadPoolExecutor settings and don't understand why the number of created threads doesn't reach the defined maximumPoolSize and stay limited to the corePoolSize.

Here are the ThreadPool settings :

public class Parallel {

    public static ThreadPoolExecutor EXECUTOR;

    static {
        ThreadPoolExecutor executorService = new ThreadPoolExecutor(5, Integer.MAX_VALUE, 1,
                TimeUnit.MINUTES, new LinkedBlockingQueue<>());
        executorService.allowsCoreThreadTimeOut();
        EXECUTOR = executorService;
    }

}

When I launch the app, I inject 7 callable tasks and expect the logs to show me that 7 threads have been created. But what I can observe is that only 5 threads have been created when 7 should be in order to parallelize all the submitted tasks.

2021-10-03 15:41:34.034 INFO  [pool-1-thread-2] Task - Thread [ pool-1-thread-2 ] doing things inside Task's call() method
2021-10-03 15:41:34.034 INFO  [pool-1-thread-4] Report - Thread [ pool-1-thread-4 ] doing things inside Report's call() method
2021-10-03 15:41:34.034 INFO  [pool-1-thread-1] Monitor - Launching [ Task | Task(number=4) ] callable
2021-10-03 15:41:34.034 INFO  [pool-1-thread-5] Task - Thread [ pool-1-thread-5 ] doing things inside Task's call() method
2021-10-03 15:41:34.034 INFO  [pool-1-thread-3] Report - Thread [ pool-1-thread-3 ] doing things inside Report's call() method
2021-10-03 15:41:34.034 INFO  [pool-1-thread-1] SuperWrapper - All callables launched
2021-10-03 15:41:34.034 INFO  [pool-1-thread-1] Task - Thread [ pool-1-thread-1 ] doing things inside Task's call() method
2021-10-03 15:41:39.039 INFO  [main] LimitWorkaround - Computing callables...
2021-10-03 15:41:44.044 INFO  [main] LimitWorkaround - Computing callables...

If I change the corePoolSize from 5 to 10, and the maximumPoolSize up to 20 (for example), the logs will show some [pool-1-thread-10] but never more than 10, which is the corePoolSize.

Is there any reason for that ?

Thanx by advance.

Lovegiver
  • 415
  • 4
  • 17

0 Answers0