gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:41:18 24 4
gpt4 key购买 nike

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

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com