我正在为ThreadPoolExecutor
设置而苦苦挣扎,不明白为什么创建的线程数没有达到定义maximumPoolSize
并限制在corePoolSize
.
以下是线程池设置:
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;
}
}
当我启动应用程序时,我注入了 7 个callable
任务,并希望日志显示已创建 7 个线程。但是我可以观察到的是,当应该并行化所有提交的任务时,只创建了 5 个线程。
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...
如果我将corePoolSize
5 更改为 10,maximumPoolSize
最多 20(例如),日志将显示一些[pool-1-thread-10]
但不会超过 10,即corePoolSize
.
有什么理由吗?
提前谢谢。