Java固定线程池和调度线程池的区别

发布于 2024-11-08 01:57:52 字数 1047 浏览 6 评论 0原文

我有一个固定线程池,可以随时运行 7 个并发线程(带有队列),我想将其变成一个仅运行 7 个并发作业但可以排队/调度更多作业的调度线程池。

阅读文档并没有真正帮助我。

newFixedThreadPool

公共静态ExecutorService newFixedThreadPool(int nThreads)

创建一个线程池,重用一组固定的运行线程 共享无界队列。如果任何线程由于失败而终止 在关闭之前执行期间,如果满足以下条件,则新的将取代它: 需要执行后续任务。

参数: nThreads - 池中的线程数返回: 新创建的线程池

newScheduledThreadPool

公共静态 ScheduledExecutorService newScheduledThreadPool(int 核心池大小)

创建一个线程池,可以安排命令在给定的时间后运行 延迟,或定期执行。

参数: corePoolSize - 池中保留的线程数,即使它们处于空闲状态。返回: 新创建的调度线程池

我不明白的是, corePoolSize 和 nThreads 是同一件事吗?调度线程池真的是固定线程池的子集吗?这意味着我可以将调度线程池用作可以对延迟任务进行排队的固定线程池?

I have a fixed thread pool that runs 7 concurrent threads at any time (with a queue), and I want to turn it into a scheduled thread pool that runs only 7 concurrent jobs but can queue/schedule more.

Reading the documentation didn't really help me..

newFixedThreadPool

public static ExecutorService newFixedThreadPool(int nThreads)

Creates a thread pool that reuses a fixed set of threads operating off
a shared unbounded queue. If any thread terminates due to a failure
during execution prior to shutdown, a new one will take its place if
needed to execute subsequent tasks.

Parameters:
nThreads - the number of threads in the pool Returns:
the newly created thread pool

newScheduledThreadPool

public static ScheduledExecutorService newScheduledThreadPool(int
corePoolSize)

Creates a thread pool that can schedule commands to run after a given
delay, or to execute periodically.

Parameters:
corePoolSize - the number of threads to keep in the pool, even if they are idle. Returns:
a newly created scheduled thread pool

What I don't understand is, are corePoolSize and nThreads the same thing? Is a scheduled thread pool really a subset of a fixed thread pool, meaning that I can use scheduled thread pool as a fixed thread pool that can queue delayed tasks?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

但可醉心 2024-11-15 01:57:52

是的,它们基本上是相同的,只是增加了调度功能。 ScheduledThreadPoolExecutor 甚至扩展了 ExecutorService (ThreadPoolExecutor) 的默认实现。

nThreads 和 corePoolSize 是要生成的线程数。对于固定的执行者来说,总是一样的。对于其他实现,它在 min (corePoolSize) 和 max (maxPoolSize) 之间变化。

Yes, they are basically the same thing, just with added scheduling functionality. The ScheduledThreadPoolExecutor even extends the default implementation of the ExecutorService (ThreadPoolExecutor).

nThreads and corePoolSize is the number of threads to be spawned. For a fixed executor, it's always the same. With the other implementation, it varies between min (corePoolSize) and max (maxPoolSize).

风蛊 2024-11-15 01:57:52

是的,在 JDK5-6 中就是这样工作的。虽然原则上 ScheduledExecutorService 接口对池大小问题保持沉默,但在 JDK 中使用的实际实现使用固定池:

ScheduledThreadPoolExecutor 类

虽然这个类继承自
ThreadPoolExecutor,其中的一些
继承的调整方法不是
对它有用。特别是,因为
它充当一个固定大小的池,使用
corePoolSize 线程和无界
队列,调整maximumPoolSize
没有任何有用的效果。

显然,如果您使用应用程序框架或不同供应商提供的 ScheduledExecutorService 的不同实现,则情况可能不成立。

Yes, it works that way in JDK5-6. While in principle the ScheduledExecutorService interface is silent on the issue of pool size, the actual implementation of it used in JDK, uses a fixed pool:

Class ScheduledThreadPoolExecutor

While this class inherits from
ThreadPoolExecutor, a few of the
inherited tuning methods are not
useful for it. In particular, because
it acts as a fixed-sized pool using
corePoolSize threads and an unbounded
queue, adjustments to maximumPoolSize
have no useful effect.

Obviously that may not hold true if you use a different implementation of ScheduledExecutorService provided by an application framework or a different vendor.

挽清梦 2024-11-15 01:57:52

是的,它们在线程池大小方面完全相同:它们 最终都调用相同的ThreadPoolExecutor 构造函数

Yes they are exactly the same with regard to thread pool size: they ultimately both call the same ThreadPoolExecutor constructor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文