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

标签 java spring multithreading threadpool

使用以下配置配置线程池有区别吗:

Executors.newFixedThreadPool(50);

与做:

ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(50);
executor.setThreadNamePrefix("thread-pool");
executor.initialize();

我对在运行时配置线程池不感兴趣(我认为这是使用 ThreadPoolTask​​Executor 的主要驱动因素)。

最佳答案

ThreadPoolTask​​Executor 是 Spring Framework 中的一个类。另一方面,Executors::newFixedThreadPool 创建了一个标准的 ThreadPoolExecutor 线程池,它来自标准 Java,自 Java 5 起可用。

来自 ThreadPoolTaskExecutor 的文档:

JavaBean that allows for configuring a ThreadPoolExecutor in bean style (through its "corePoolSize", "maxPoolSize", "keepAliveSeconds", "queueCapacity" properties) and exposing it as a Spring TaskExecutor.

....

This class implements Spring's TaskExecutor interface as well as the Executor interface, with the former being the primary interface, the other just serving as secondary convenience. For this reason, the exception handling follows the TaskExecutor contract rather than the Executor contract, in particular regarding the TaskRejectedException.

请注意,ThreadPoolTask​​Executor 实现了许多 Spring 接口(interface),例如 AwareBeanNameAwareDisposableBeanInitializingBean 这使得像 Spring bean 这样的池更容易工作。

另请查看 Karol Dowbecki's answer它正确地指出了这些池参数的差异。

关于java - FixedThreadPool 和 ThreadPoolTask​​Executor 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57260217/

相关文章:

c++ - 通过 std::thread 传递右值

java版本值与java和javac不同

java - 将滚动事件发送到另一个应用程序

java - 无法构造 java.util.Collection xstream

spring - 构建后、部署前的属性文件配置

java - 如果条件不适用于相关实体

java - 字符串搜索的性能

java - Spring JPA 和 Streaming - 数据是否增量获取?

java - 不必要的 Java 上下文切换

.net - C# 线程和 SQL 连接