繁体   English   中英

FixedThreadPool和ThreadPoolTask​​Executor有什么区别?

[英]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起可用。

ThreadPoolTask​​Executor的文档:

允许以bean样式配置ThreadPoolExecutor JavaBean(通过其“corePoolSize”,“maxPoolSize”,“keepAliveSeconds”,“queueCapacity”属性)并将其公开为Spring TaskExecutor

....

这个类实现了Spring的TaskExecutor接口以及Executor接口,前者是主接口,另一个只是作为次要的便利。 因此,异常处理遵循TaskExecutor契约而不是Executor契约,特别是关于TaskRejectedException

请注意, ThreadPoolTaskExecutor实现了许多Spring接口,如AwareBeanNameAwareDisposableBeanInitializingBean ,这使得使用Spring bean这样的池更容易。

另请参阅Karol Dowbecki的答案 ,该答案正确指出了这些池参数的差异。

在你的例子中,Spring的ThreadPoolTaskExecutor将创建一个ThreadPoolExecutor ,其corePoolSize为50, maxPoolSizeInteger.MAX_VALUEkeepAlive为60秒

同时Executors.newFixedThreadPool(50)corePoolSizemaxPoolSize设置为50并且keepAlive为0秒(参见Java源代码)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM