Java固定线程池和调度线程池的区别
我有一个固定线程池,可以随时运行 7 个并发线程(带有队列),我想将其变成一个仅运行 7 个并发作业但可以排队/调度更多作业的调度线程池。
阅读文档并没有真正帮助我。
公共静态ExecutorService newFixedThreadPool(int nThreads)
创建一个线程池,重用一组固定的运行线程 共享无界队列。如果任何线程由于失败而终止 在关闭之前执行期间,如果满足以下条件,则新的将取代它: 需要执行后续任务。
参数: nThreads - 池中的线程数返回: 新创建的线程池
公共静态 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..
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
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,它们基本上是相同的,只是增加了调度功能。 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).
是的,在 JDK5-6 中就是这样工作的。虽然原则上 ScheduledExecutorService 接口对池大小问题保持沉默,但在 JDK 中使用的实际实现使用固定池:
ScheduledThreadPoolExecutor 类
显然,如果您使用应用程序框架或不同供应商提供的 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
Obviously that may not hold true if you use a different implementation of ScheduledExecutorService provided by an application framework or a different vendor.
是的,它们在线程池大小方面完全相同:它们 最终都调用相同的ThreadPoolExecutor 构造函数。
Yes they are exactly the same with regard to thread pool size: they ultimately both call the same ThreadPoolExecutor constructor.