FixedThreadPool vs CachedThreadPool:两个邪恶中较小的一个 [英] FixedThreadPool vs CachedThreadPool: the lesser of two evils

查看:90
本文介绍了FixedThreadPool vs CachedThreadPool:两个邪恶中较小的一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个生成线程(~5-150)的程序,它执行一堆任务。最初我使用 FixedThreadPool ,因为这个类似的问题建议他们更适合长寿命的任务,而且由于我对多线程的了解非常有限,我认为线程的平均寿命(几分钟)长寿

So I have a program that spawns threads (~5-150) which perform a bunch of tasks. Originally I used a FixedThreadPool because this similar question suggested they were better suited for longer lived tasks and with my very limited knowledge of multithreading, I considered the average life of the threads (several minutes) "long lived".

但是,我最近添加了生成其他线程的功能,这样做会使我超出我设置的线程限制。在这种情况下,最好猜测并增加我可以允许的线程数或切换到 CachedThreadPool 所以我没有浪费线程?

However, I recently added the capability to spawn additional threads and doing so takes me above the thread limit I set. In this case, would it be better to guess and increase the number threads I can allow or to switch to a CachedThreadPool so I have no wasted threads?

初步尝试这两种方法,似乎是不同的,所以我倾向于使用 CachedThreadPool 只是为了避免浪费。但是,线程的生命周期是否意味着我应该选择 FixedThreadPool 并处理未使用的线程? 这个问题让它看起来像那些额外的线程浪费但我很感激澄清。

Trying them both out preliminarily, there doesn't seem to be a difference so I'm inclined to go with the CachedThreadPool just to avoid the waste. However, does the life span of the threads mean I should instead picked a FixedThreadPool and just deal with the unused threads? This question makes it seem like those extra threads aren't wasted but I would appreciate the clarification.

推荐答案

CachedThreadPool正是你应该用于你的情况,因为没有使用一个用于长时间运行的线程的负面后果。关于CachedThreadPools的Java文档中适用于简短任务的注释仅表明它们特别适用于此类情况,而不是它们不能或不应该用于涉及长时间运行任务的任务。

A CachedThreadPool is exactly what you should use for your situation as there are no negative consequence to using one for long running threads. The comment in the java doc about CachedThreadPools being suitable for short tasks merely suggest that they are particularly appropriate for such cases, not that they cannot or should not be used for tasks involving long running tasks.

进一步详细说明, Executors.newCachedThreadPool Executors.newFixedThreadPool 都由相同的线程池实现(至少在开放的JDK中)支持,只是使用不同的参数。差异只是它们的线程最小值,最大值,线程终止时间和队列类型。

To elaborate further, Executors.newCachedThreadPool and Executors.newFixedThreadPool are both backed by the same thread pool implementation (at least in the open JDK) just with different parameters. The differences just being their thread minimum, maximum, thread kill time, and queue type.

public static ExecutorService newFixedThreadPool(int nThreads) {
     return new ThreadPoolExecutor(nThreads, nThreads,
                                   0L, TimeUnit.MILLISECONDS,
                                   new LinkedBlockingQueue<Runnable>());
 }

public static ExecutorService newCachedThreadPool() {
    return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                 60L, TimeUnit.SECONDS,
                                 new SynchronousQueue<Runnable>());
}

当你确实想要使用它时,FixedThreadPool确实有它的优势固定数量的线程,从那时起,您可以向执行程序服务提交任意数量的任务,同时知道线程数将保持在您指定的级别。如果您明确希望增加线程数,那么这不是合适的选择。

A FixedThreadPool does have its advantages when you do in fact want to work with a fixed number of threads, since then you can submit any number of tasks to the executor service while knowing that the number of threads will be maintained at the level you specified. If you explicitly want to grow the number of threads, then this is not the appropriate choice.

但这意味着您可能对CachedThreadPool提出的一个问题是关于限制并发运行的线程数。 CachedThreadPool不会为您限制它们,因此您可能需要编写自己的代码以确保不会运行太多线程。这实际上取决于您的应用程序的设计以及如何将任务提交给执行程序服务。

This does however mean that the one issue that you may have with the CachedThreadPool is in regards to limiting the number of threads that are running concurrently. The CachedThreadPool will not limit them for you, so you may need to write your own code to ensure that you do not run too many threads. This really depends on the design of your application and how tasks are submitted to the executor service.

这篇关于FixedThreadPool vs CachedThreadPool:两个邪恶中较小的一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆