java - invokeAll 的参数(集合)是否取决于队列大小?

标签 java

据我所知,我可以向invokeAll提交可调用线程的集合,线程池将执行所有任务(在任何给定点并行运行等于线程池大小的任务)。

但是在使用 invokeall 时我真的需要处理队列溢出吗?

最佳答案

这完全取决于您的代码和业务需求。 javadoc for ThreadPoolExecutor陈述了以下与队列有关的用例:

There are three general strategies for queuing:

Direct handoffs. A good default choice for a work queue is a SynchronousQueue that hands off tasks to threads without otherwise holding them. Here, an attempt to queue a task will fail if no threads are immediately available to run it, so a new thread will be constructed. This policy avoids lockups when handling sets of requests that might have internal dependencies. Direct handoffs generally require unbounded maximumPoolSizes to avoid rejection of new submitted tasks. This in turn admits the possibility of unbounded thread growth when commands continue to arrive on average faster than they can be processed.

Unbounded queues. Using an unbounded queue (for example a LinkedBlockingQueue without a predefined capacity) will cause new tasks to wait in the queue when all corePoolSize threads are busy. Thus, no more than corePoolSize threads will ever be created. (And the value of the maximumPoolSize therefore doesn't have any effect.) This may be appropriate when each task is completely independent of others, so tasks cannot affect each others execution; for example, in a web page server. While this style of queuing can be useful in smoothing out transient bursts of requests, it admits the possibility of unbounded work queue growth when commands continue to arrive on average faster than they can be processed.

Bounded queues. A bounded queue (for example, an ArrayBlockingQueue) helps prevent resource exhaustion when used with finite maximumPoolSizes, but can be more difficult to tune and control. Queue sizes and maximum pool sizes may be traded off for each other: Using large queues and small pools minimizes CPU usage, OS resources, and context-switching overhead, but can lead to artificially low throughput. If tasks frequently block (for example if they are I/O bound), a system may be able to schedule time for more threads than you otherwise allow. Use of small queues generally requires larger pool sizes, which keeps CPUs busier but may encounter unacceptable scheduling overhead, which also decreases throughput.

关于java - invokeAll 的参数(集合)是否取决于队列大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37111755/

相关文章:

java - log4j 日志文件不更新

java - 在Processing/Java的链接列表中找到5个最大的值

Windows 上带有通配符的 java 类路径 : Requires semi-colon at the end

java - GUI Java 程序 - Paint 程序

java - Joshua Bloch 在 Effective Java 中解释的枚举类型

java - 接收器到警报 Activity 包返回 null,即使分配了默认包值

java - 异常后丢失EntityManager

java - Weblogic EJB 调用在中等负载下开始失败并出现OptionalDataException

java - 当我单击 eclipse.exe 时,它​​没有运行并显示错误?

java - 重写子类中的方法,同时保留父类(super class)方法的内容