[英]What is the difference between corePoolSize and maxPoolSize in the Spring ThreadPoolTaskExecutor
[英]What is the difference between a FixedThreadPool and ThreadPoolTaskExecutor?
使用以下配置配置线程池之间是否存在差异:
Executors.newFixedThreadPool(50);
与做:
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(50);
executor.setThreadNamePrefix("thread-pool");
executor.initialize();
我对在运行时配置线程池不感兴趣(我认为这是使用ThreadPoolTaskExecutor
的主要驱动程序)。
ThreadPoolTaskExecutor
是Spring Framework的一个类。 另一方面, Executors::newFixedThreadPool
创建一个标准的ThreadPoolExecutor
线程池,它来自标准Java,自Java 5起可用。
允许以bean样式配置
ThreadPoolExecutor
JavaBean(通过其“corePoolSize”,“maxPoolSize”,“keepAliveSeconds”,“queueCapacity”属性)并将其公开为SpringTaskExecutor
。....
这个类实现了Spring的
TaskExecutor
接口以及Executor
接口,前者是主接口,另一个只是作为次要的便利。 因此,异常处理遵循TaskExecutor
契约而不是Executor
契约,特别是关于TaskRejectedException
。
请注意, ThreadPoolTaskExecutor
实现了许多Spring接口,如Aware
, BeanNameAware
, DisposableBean
, InitializingBean
,这使得使用Spring bean这样的池更容易。
另请参阅Karol Dowbecki的答案 ,该答案正确指出了这些池参数的差异。
在你的例子中,Spring的ThreadPoolTaskExecutor
将创建一个ThreadPoolExecutor
,其corePoolSize
为50, maxPoolSize
为Integer.MAX_VALUE
, keepAlive
为60秒 。
同时Executors.newFixedThreadPool(50)
将corePoolSize
和maxPoolSize
设置为50并且keepAlive
为0秒(参见Java源代码)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.