简体   繁体   English

Fork-Join线程池中的长时间运行任务是否可以阻止所有线程?

[英]Can a long running task in a Fork-Join thread pool block all threads?

I've been learning about Java 8's parallel streams and this article came up on the first page of Google search. 我一直在学习Java 8的并行流, 这篇文章出现在Google搜索的第一页上。 I am unable to understand this particular line from the article 我无法理解文章中的这一特定内容

...all parallel streams use common fork-join thread pool and if you submit a long-running task, you effectively block all threads in the pool. ...所有并行流都使用公共fork-join线程池,如果您提交长时间运行的任务,则可以有效地阻止池中的所有线程

I can't figure out how a long running task can block all other threads in a pool. 我无法弄清楚长时间运行的任务如何阻止池中的所有其他线程。

In this statement “submit a long-running task” means “perform a long-running task with a parallel stream”. 在此语句中,“提交长时间运行的任务”意味着“使用并行流执行长时间运行的任务”。 It's the intention of parallel stream processing, to split the work and distribute at to all worker threads of the common thread pool. 这是并行流处理的目的 ,将工作拆分并分配到公共​​线程池的所有工作线程。

This pool has a number of threads configured to utilize all CPU cores and if all cores are busy, that goal has been reached. 此池具有许多配置为使用所有CPU核心的线程,如果所有核心都忙,则已达到该目标。

A problem arises if these threads get blocked by waiting for the completion of an I/O operation. 如果通过等待I / O操作的完成来阻止这些线程,则会出现问题。 Then, the CPU cores are not utilized, which may not only degrade the performance of other parallel streams, even within the same operation, the optimal number of concurrent I/O operations may be entirely different than the number of CPU cores. 然后,不利用CPU核心,这不仅会降低其他并行流的性能,即使在相同的操作中,并发I / O操作的最佳数量可能与CPU核心的数量完全不同。

The conclusion is that the Stream API is not suitable for parallel I/O operations. 结论是Stream API不适合并行I / O操作。

Submitting one task can not use all the threads in the common-pool. 提交一个任务不能使用公共池中的所有线程。 common-pool size is core count -1 (-1 for the main thread). common-pool size是core count -1 (主线程为-1)。

I think in the article, he meant it is not possible(in the API) to separate io intensive tasks and cpu intensive tasks to different pools. 我认为在文章中,他的意思是(在API中)不可能将io密集型任务和cpu密集型任务分离到不同的池中。
For example, On a 8 core machine, If you submit 8 cpu intensive tasks to the pool then 8 io intensive tasks, io task have to wait cpu intensive tasks to finish(or vice versa). 例如,在8核计算机上,如果向池中提交8个cpu密集型任务,那么8个io密集型任务,io任务必须等待cpu密集型任务完成(反之亦然)。 If you could submit them to different pools all 16 operation could run concurrently, because io operation does not costly on the cpu. 如果你可以将它们提交到不同的池,则所有16个操作可以同时运行,因为io操作在cpu上并不昂贵。

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

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