Executors.newCachedThreadPool()与Executors.newFixedThreadPool() [英] Executors.newCachedThreadPool() versus Executors.newFixedThreadPool()

查看:364
本文介绍了Executors.newCachedThreadPool()与Executors.newFixedThreadPool()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

newCachedThreadPool() newFixedThreadPool()

我应该何时使用非此即彼?哪种策略在资源利用方面更好?

When should I use one or the other? Which strategy is better in terms of resource utilization?

推荐答案

我认为文档很好地解释了这两个函数的区别和用法:

I think the docs explain the difference and usage of these two functions pretty well:

newFixedThreadPool


创建一个线程池,该池重用
共享无界队列的
固定数量的线程。在任何
点,最多nThreads线程将
作为活动处理任务。如果在
所有线程都处于活动状态时提交
其他任务,它们将在队列中等待
,直到线程为
可用。如果任何线程由于在关闭之前执行
期间的失败而终止
,则如果需要执行
后续任务,则新线程将花费

池中的线程将一直存在,直到显式
shutdown。

Creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue. At any point, at most nThreads threads will be active processing tasks. If additional tasks are submitted when all threads are active, they will wait in the queue until a thread is available. 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. The threads in the pool will exist until it is explicitly shutdown.

newCachedThreadPool


创建一个线程池,根据需要创建新的
线程,但是当
可用时,将重用
之前构造的线程。这些池
通常会提高执行许多短期
异步任务的
程序的性能。执行
的调用将重用以前构造的
线程(如果可用)。如果没有现有的
线程可用,则新线程将创建
并添加到池中。
终止未用于
六十秒的线程,并从缓存中删除
。因此,保持闲置足够长的池
将不会消耗任何资源
。请注意,可以使用ThreadPoolExecutor构造函数创建具有类似属性但
不同详细信息的
池(例如,
超时参数)

Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available. These pools will typically improve the performance of programs that execute many short-lived asynchronous tasks. Calls to execute will reuse previously constructed threads if available. If no existing thread is available, a new thread will be created and added to the pool. Threads that have not been used for sixty seconds are terminated and removed from the cache. Thus, a pool that remains idle for long enough will not consume any resources. Note that pools with similar properties but different details (for example, timeout parameters) may be created using ThreadPoolExecutor constructors.

在资源方面, newFixedThreadPool 将保持所有线程运行,直到它们被明确终止。在 newCachedThreadPool 中,未使用60秒的线程终止并从缓存中删除。

In terms of resources, the newFixedThreadPool will keep all the threads running until they are explicitly terminated. In the newCachedThreadPool Threads that have not been used for sixty seconds are terminated and removed from the cache.

鉴于此,资源消耗将在很大程度上取决于情况。例如,如果你有大量长时间运行的任务,我会建议 FixedThreadPool 。对于 CachedThreadPool ,文档说这些池通常会提高执行许多短期异步任务的程序的性能。

Given this, the resource consumption will depend very much in the situation. For instance, If you have a huge number of long running tasks I would suggest the FixedThreadPool. As for the CachedThreadPool, the docs say that "These pools will typically improve the performance of programs that execute many short-lived asynchronous tasks".

这篇关于Executors.newCachedThreadPool()与Executors.newFixedThreadPool()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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