使用以下配置配置线程池有区别吗:
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 起可用。
来自 ThreadPoolTaskExecutor 的文档:
JavaBean that allows for configuring a
ThreadPoolExecutor
in bean style (through its "corePoolSize", "maxPoolSize", "keepAliveSeconds", "queueCapacity" properties) and exposing it as a SpringTaskExecutor
.....
This class implements Spring's
TaskExecutor
interface as well as theExecutor
interface, with the former being the primary interface, the other just serving as secondary convenience. For this reason, the exception handling follows theTaskExecutor
contract rather than theExecutor
contract, in particular regarding theTaskRejectedException
.
请注意,ThreadPoolTaskExecutor
实现了许多 Spring 接口(interface),例如 Aware
、BeanNameAware
、DisposableBean
、InitializingBean
这使得像 Spring bean 这样的池更容易工作。
另请查看 Karol Dowbecki's answer它正确地指出了这些池参数的差异。
关于java - FixedThreadPool 和 ThreadPoolTaskExecutor 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57260217/