Java,哪个线程是顺序流执行的? [英] Java, in which thread are sequential streams executed?

查看:142
本文介绍了Java,哪个线程是顺序流执行的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读有关溪流的文档时,我遇到了以下句子:

While reading the documentation about streams, I came across the following sentences:



。 ..试图从行为参数访问可变状态会给你一个错误的选择...如果你没有同步访问该状态,你就会有数据竞争,因此你的代码被破坏了...... [1]

... attempting to access mutable state from behavioral parameters presents you with a bad choice ... if you do not synchronize access to that state, you have a data race and therefore your code is broken ... [1]



  • 如果行为参数确实有副作用...... [没有]保证不同的操作在同一个流管道中的相同元素上,在同一个线程中执行。 [2]

    If the behavioral parameters do have side-effects ... [there are no] guarantees that different operations on the "same" element within the same stream pipeline are executed in the same thread. [2]



  • 对于任何给定元素,可以在任何时间执行操作,在库选择的任何线程中。 [3]

    For any given element, the action may be performed at whatever time and in whatever thread the library chooses. [3]


  • 这些句子不区分顺序和并行流。所以我的问题是:

    These sentences don't make a distinction between sequential and parallel streams. So my questions are:


    1. 在哪个线程中执行顺序流的管道?它总是调用线程还是可以自由选择任何线程的实现?

    2. 如果流是顺序的,那么在哪个线程中执行forEach终端操作的action参数?

    3. 使用顺序流时是否必须使用任何同步?







    • [1 + 2] https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html

    • [3] https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#forEach-java.util.function.Consumer-


      • [1+2] https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html
      • [3] https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#forEach-java.util.function.Consumer-
      • 推荐答案


        1. Stream的终端操作是阻止操作。如果没有并行执行,执行终端操作的线程将运行管道中的所有操作。

        1. Stream's terminal operations are blocking operations. In case there is no parallel excution, the thread that executes the terminal operation runs all the operations in the pipeline.




        定义1.1。 管道 是一些链式方法。

        Definition 1.1. Pipeline is a couple of chained methods.


        定义1.2。 中间操作 将位于流末端的所有位置。它们返回一个流对象并且不会在管道中执行任何操作。

        Definition 1.2. Intermidiate operations will be located everywhere in the stream expect at the end. They return a stream object and does not excute any operation in the pipeline.


        定义1.3。 终端操作 将仅位于流的末尾。他们执行管道。它们不返回流对象,因此不能在它们之后添加其他中间操作终端操作




        1. 从第一个解决方案我们可以得出结论,调用线程将执行内部的 action 方法对于呼叫流中的每个元素, forEach 终端操作

        1. From the first solution we can conclude that the calling thread will execute the action method inside the forEach terminal operation on each element in the calling stream.

        Java 8向我们介绍了 Spliterator 接口。它具有 Iterator 的功能,但也有一组操作可以帮助并行执行和分割任务。

        Java 8 introduces us the Spliterator interface. It has the capabilities of Iterator but also a set of operations to help performing and spliting a task in parallel.

        在顺序执行中从原始流调用 forEach 时,调用线程将调用 Spliterator.forEachRemaining 方法:

        When calling forEach from primitive streams in sequential execution, the calling thread will invoke the Spliterator.forEachRemaining method:

        @Override
        public void forEach(IntConsumer action) {
           if (!isParallel()) {
                adapt(sourceStageSpliterator()).forEachRemaining(action);
            }
            else {
                super.forEach(action);
            }
        }
        

        您可以在上阅读更多信息我的教程中的Spliterator 第7章:Spliterator


        1. 只要你不改变其中一个流操作中多个线程之间的任何共享状态(并且它是禁止的 - 很快就会解释,当你想运行并行流时,你不需要使用任何额外的同步工具或算法。

        流操作,如reduce使用 accumulator combiner 执行并行流的函数。根据定义,流库禁止变异。你应该避免它。

        Stream operations like reduce use accumulator and combiner functions for executing parallel streams. The streams library by definition forbids mutation. You should avoid it.

        并发和并行编程中有很多定义。我将介绍一组最适合我们的定义。

        There are a lot of definitions in concurrent and parallel programming. I will introduce a set of definitions that will serve us best.


        定义8.1。 Cuncurrent programming 是使用其他同步算法解决任务的能力。

        Definition 8.1. Cuncurrent programming is the ability to solve a task using additional synchronization algorithms.


        定义8.2。 并行编程 是解决问题的能力没有使用其他同步算法的任务。

        Definition 8.2. Parallel programming is the ability to solve a task without using additional synchronization algorithms.

        您可以在我的教程中阅读更多相关信息:第8章:并行流

        You can read more about it in my tutorial: Chapter 8: Parallel Streams.

        这篇关于Java,哪个线程是顺序流执行的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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