默认执行的是串行的。
/** * An operation in a stream pipeline that takes a stream as input and produces * a result or side-effect. A {@code TerminalOp} has an input type and stream * shape, and a result type. A {@code TerminalOp} also has a set of * <em>operation flags</em> that describes how the operation processes elements * of the stream (such as short-circuiting or respecting encounter order; see * {@link StreamOpFlag}). 流管道中的一个操作。会接受一个流作为输入, 产生的结果,是有副作用的(副作用:你传递了一个引用,你修改了这个引用)。 一个 TerminalOp 会有一个输入类型,和流的shape 和一个结果类型。 TerminalOp 还会有一个 如何处理流中的元素 的标识。 TerminalOp 必须要提供一种 串行的和并行的 实现。 * * <p>A {@code TerminalOp} must provide a sequential and parallel implementation * of the operation relative to a given stream source and set of intermediate * operations. * * @param <E_IN> the type of input elements * @param <R> the type of the result * @since 1.8 */ interface TerminalOp<E_IN, R> { /** * Gets the shape of the input type of this operation. * * @implSpec The default returns {@code StreamShape.REFERENCE}. * * @return StreamShape of the input type of this operation */ default StreamShape inputShape() { return StreamShape.REFERENCE; } /** * Gets the stream flags of the operation. Terminal operations may set a * limited subset of the stream flags defined in {@link StreamOpFlag}, and * these flags are combined with the previously combined stream and * intermediate operation flags for the pipeline. * * @implSpec The default implementation returns zero. * * @return the stream flags for this operation * @see StreamOpFlag */ default int getOpFlags() { return 0; } /** * Performs a parallel evaluation of the operation using the specified * {@code PipelineHelper}, which describes the upstream intermediate * operations. * * @implSpec The default performs a sequential evaluation of the operation * using the specified {@code PipelineHelper}. * * @param helper the pipeline helper * @param spliterator the source spliterator * @return the result of the evaluation */ default <P_IN> R evaluateParallel(PipelineHelper<E_IN> helper, Spliterator<P_IN> spliterator) { if (Tripwire.ENABLED) Tripwire.trip(getClass(), "{0} triggering TerminalOp.evaluateParallel serial default"); return evaluateSequential(helper, spliterator); } /** * Performs a sequential evaluation of the operation using the specified * {@code PipelineHelper}, which describes the upstream intermediate * operations. * * @param helper the pipeline helper * @param spliterator the source spliterator * @return the result of the evaluation */ <P_IN> R evaluateSequential(PipelineHelper<E_IN> helper, Spliterator<P_IN> spliterator); }终止操作的实现就4类:
1.find
2.match
3.forEach 遍历
4.reduce
返回去: forEach()操作就是返回了一个终止操作对象。
然后:evaluate()方法, 执行那个终止操作对象。
// Terminal evaluation methods /** * Evaluate the pipeline with a terminal operation to produce a result. * * @param <R> the type of result * @param terminalOp the terminal operation to be applied to the pipeline. * @return the result */ final <R> R evaluate(TerminalOp<E_OUT, R> terminalOp) { assert getOutputShape() == terminalOp.inputShape(); if (linkedOrConsumed) throw new IllegalStateException(MSG_STREAM_LINKED); linkedOrConsumed = true; return isParallel() ? terminalOp.evaluateParallel(this, sourceSpliterator(terminalOp.getOpFlags())) : terminalOp.evaluateSequential(this, sourceSpliterator(terminalOp.getOpFlags())); }PipelineHelper类
用来描述流的各种信息。
/** * Helper class for executing <a href="package-summary.html#StreamOps"> * stream pipelines</a>, capturing all of the information about a stream * pipeline (output shape, intermediate operations, stream flags, parallelism, * etc) in one place. Helper是一个帮助类,用于执行流管道 包含流管道的所有信息:源数据。输出类型,操作,流标识,并行标记等。 * * <p> * A {@code PipelineHelper} describes the initial segment of a stream pipeline, * including its source, intermediate operations, and may additionally * incorporate information about the terminal (or stateful) operation which * follows the last intermediate operation described by this * {@code PipelineHelper}. The {@code PipelineHelper} is passed to the * {@link TerminalOp#evaluateParallel(PipelineHelper, java.util.Spliterator)}, * {@link TerminalOp#evaluateSequential(PipelineHelper, java.util.Spliterator)}, * and {@link AbstractPipeline#opEvaluateParallel(PipelineHelper, java.util.Spliterator, * java.util.function.IntFunction)}, methods, which can use the * {@code PipelineHelper} to access information about the pipeline such as * head shape, stream flags, and size, and use the helper methods * such as {@link #wrapAndCopyInto(Sink, Spliterator)}, * {@link #copyInto(Sink, Spliterator)}, and {@link #wrapSink(Sink)} to execute * pipeline operations.. 一个流管道的最初的分块,包含源,中间操作和增加的操作。等 PipelineHelper会被传递给。。。。 方法, 就可以通过PipelineHelper来访问管道的各种信息。 * * @param <P_OUT> type of output elements from the pipeline * @since 1.8 */ abstract class PipelineHelper<P_OUT> { ... }