简体   繁体   English

固定线程池和计划线程池之间的Java差异

[英]Java difference between fixed threadpool and scheduled threadpool

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. 我有一个固定的线程池,该线程池可随时(有一个队列)运行7个并发线程,并且我想将其变成仅运行7个并发作业但可以排队/调度更多的预定线程池。

Reading the documentation didn't really help me.. 阅读文档并没有真正帮助我。

newFixedThreadPool newFixedThreadPool

public static ExecutorService newFixedThreadPool(int nThreads) 公共静态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 参数:nThreads-池中的线程数返回:新创建的线程池

newScheduledThreadPool newScheduledThreadPool

public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) 公共静态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. 参数:corePoolSize-即使处于空闲状态也要保留在池中的线​​程数。 Returns: a newly created scheduled thread pool 返回:新创建的预定线程池

What I don't understand is, are corePoolSize and nThreads the same thing? 我不明白的是,corePoolSize和nThreads是同一件事吗? 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? 预定线程池真的是固定线程池的子集,这意味着我可以将预定线程池用作可以将延迟任务排队的固定线程池吗?

Yes, they are basically the same thing, just with added scheduling functionality. 是的,只是增加了调度功能,它们基本上是同一件事。 The ScheduledThreadPoolExecutor even extends the default implementation of the ExecutorService (ThreadPoolExecutor). ScheduledThreadPoolExecutor甚至扩展了ExecutorService(ThreadPoolExecutor)的默认实现。

nThreads and corePoolSize is the number of threads to be spawned. nThreads和corePoolSize是要产生的线程数。 For a fixed executor, it's always the same. 对于固定的执行者,总是一样的。 With the other implementation, it varies between min (corePoolSize) and max (maxPoolSize). 通过其他实现,它在min(corePoolSize)和max(maxPoolSize)之间变化。

Yes, it works that way in JDK5-6. 是的,它在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: 虽然原则上ScheduledExecutorService接口在池大小的问题上保持沉默,但在JDK中使用的实际实现使用固定池:

Class ScheduledThreadPoolExecutor 类ScheduledThreadPoolExecutor

While this class inherits from ThreadPoolExecutor, a few of the inherited tuning methods are not useful for it. 尽管此类从ThreadPoolExecutor继承,但是一些继承的调整方法对此没有用。 In particular, because it acts as a fixed-sized pool using corePoolSize threads and an unbounded queue, adjustments to maximumPoolSize have no useful effect. 特别是,由于它使用corePoolSize线程和无限制队列充当固定大小的池,因此对maximumPoolSize的调整没有任何作用。

Obviously that may not hold true if you use a different implementation of ScheduledExecutorService provided by an application framework or a different vendor. 显然,如果您使用应用程序框架或其他供应商提供的ScheduledExecutorService的其他实现,则可能不成立。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM