java Java固定线程池和调度线程池的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6037693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java difference between fixed threadpool and scheduled threadpool
提问by Mohamed Nuur
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..
阅读文档并没有真正帮助我..
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
公共静态 ExecutorService newFixedThreadPool(int nThreads)
创建一个线程池,该线程池重用一组固定的线程,在共享的无界队列中运行。如果任何线程在关闭前的执行过程中由于失败而终止,则在需要执行后续任务时,将有一个新线程代替它。
参数: nThreads - 线程池中的线程数返回:新创建的线程池
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
公共静态 ScheduledExecutorService newScheduledThreadPool(int corePoolSize)
创建一个线程池,可以安排命令在给定延迟后运行,或定期执行。
参数: corePoolSize - 要保留在池中的线程数,即使它们处于空闲状态。返回:一个新创建的调度线程池
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?
我不明白的是,corePoolSize 和 nThreads 是一回事吗?调度线程池真的是固定线程池的一个子集,也就是说我可以将调度线程池用作固定线程池,可以对延迟任务进行排队?
采纳答案by mhaller
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. For a fixed executor, it's always the same. With the other implementation, it varies between min (corePoolSize) and max (maxPoolSize).
nThreads 和 corePoolSize 是要产生的线程数。对于固定执行器,它总是相同的。对于其他实现,它在 min (corePoolSize) 和 max (maxPoolSize) 之间变化。
回答by Affe
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:
是的,它在 JDK5-6 中就是这样工作的。虽然原则上 ScheduledExecutorService 接口对池大小问题保持沉默,但它在 JDK 中使用的实际实现使用固定池:
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.
虽然这个类继承自 ThreadPoolExecutor,但一些继承的调优方法对它没有用。特别是,因为它使用 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 的不同实现,这可能不成立。
回答by Peter Knego
Yes they are exactly the same with regard to thread pool size: they ultimately both callthe same ThreadPoolExecutor constructor.
是的,它们在线程池大小方面完全相同:它们最终都调用相同的ThreadPoolExecutor 构造函数。