93 可读性
 将光标移到/点击文章中的句子上,可以查看译文。      显示繁体中文内容    显示简体中文内容

Where does official documentation say that Java's parallel stream operations use fork/join?
官方文档认为Java流操作的并行使用 fork/join?

113 1

Here's my understanding of the Stream framework of Java 8 :

  • Something creates a source Stream
  • The implementation is responsible for providing a BaseStream#parallel() method, which in turns returns a Stream that can run it's operations in parallel.

While someone has already found a way to use a custom thread pool with Stream framework's parallel executions, i cannot for the life of me find any mention in the Java 8 API that the default Java 8 parallel Stream implementations would use ForkJoinPool#commonPool().( Collection#parallelStream(), the methods in StreamSupport class, and others possible sources of parallel-enabled streams in the API that i don't know about).

Only tidbits that i could gleam off search results were these :

  • State of the Lambda :Libraries Edition ("Parallelism under the hood")
    Vaguely mentions the Stream framework and the Fork/Join machinery.

    the Fork/Join machinery is designed to automate this process.

  • JEP 107 : Bulk Data Operations for Collections
    Almost directly states that the the Collection interface's default method #parallelStream() implements itself using Fork/Join.but still nothing about common pool.

    the parallel implementation builds upon the java.util.concurrency Fork/Join implementation introduced in Java 7.

    and hence : Collection#parallelStream().

  • Class Arrays (Javadoc)
    Directly states multiple times that the common pool is used.

    the ForkJoin common pool is used to execute any parallel tasks.

so my question is :

Where is it said that the ForkJoinPool#commonPool() is used for parallel operations on streams that are obtained from the Java 8 API?

时间:原作者:0个回答

101 0

For what it's worth, Java 8 in Action has a chapter on Parallel data processing and performance (Chapter 7 ).it says :

".. .the Stream interface gives you the opportunity to execute operations in parallel on a collection of data without much effort."

".. .you'll see how Java can make this magic happen or, more practically, how parallel streams work under the hood by employing the fork/join framework introduced in Java 7."

It also has a small side note in section 7.1 :

" Parallel streams internally use the default ForkJoinPool...which by default has as many threads as you have processors, as returned byRuntime.getRuntime().availableProcessors()."

" you can change the size of this pool using the system property java.util. concurrent.ForkJoinPool.common.parallelism, as in the following example:"

System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism","12");

As mentioned in the comments and other answers, this does not mean it will always use the fork/join.

原作者:
...