Home >Java >javaTutorial >Difference between Parallel and Sequential Streams in Java
Java 8 introduces Stream, located in the java.util.stream
package. Stream is a sequence of objects, similar to an array or collection, and supports a variety of methods and aggregation operations, including filtering, mapping, reduction, limiting, matching, and finding. . These operations do not modify the original data source, but create a new Stream for processing. Streams are mainly divided into two types: Sequential Stream and Parallel Stream. This article will focus on the differences between the two.
Sequential streams use a single thread to process data in the pipeline. Objects in the sequential stream are located in the same processing system and arranged in order, so they are not processed using a multi-core system.
Parallel streams utilize multi-core processors to improve program performance. The code is divided into multiple streams through a parallel stream method that are executed in parallel on different kernels. The final results are displayed after merge. Since the execution is not controlled by the developer, the results may be disordered. Parallel flows can be used in the following ways:
Collection
interface contains the parallelStream()
method to create parallel streams. BaseStream
interface contains the parallel()
method that can be used to convert sequential streams into parallel streams. The following table summarizes the main differences between sequential streams and parallel streams in Java:
顺序流 | 并行流 |
---|---|
在计算机的单个内核上执行。 | 在计算机的多个内核上执行。 |
性能较慢。 | 性能较快。 |
执行过程中保持顺序。 | 执行过程中不保证顺序。 |
一次只能进行单个迭代。 | 由于在多个内核上执行,可以进行多个迭代。 |
每个迭代必须等待前一个迭代完成才能执行。 | 如果所有内核都繁忙,则流必须等待;否则,它们将同时执行而无需等待。 |
出错概率较低。 | 出错概率较高。 |
与平台无关。 | 依赖于平台。 |
Stream in Java is mainly divided into sequential streams and parallel streams. Sequential streams perform objects in sequence, with orderly results, executed using a single kernel of the computer, slow performance, but not platform-related. Parallel streams are executed using multiple cores of the computer, and the performance is fast, but the result is out of order.
Sequential streaming is platform-related, as it only uses a single kernel to execute code. Parallel streams use multiple kernels to execute code, so they depend on the platform.
Parallel streams are more error-prone because they run on multiple cores of the computer and the results are out of order. The probability of sequential streaming errors is lower because it only uses a single kernel to execute code.
In sequential streams, only one iteration can be performed at a time. The next iteration must wait for the current iteration to complete. In parallel streams, iterations work simultaneously on different kernels. If all kernels are busy, the iteration must wait.
Sequential streams remain in sequence during execution, because it only uses one kernel, and each iteration must wait for the current iteration to complete execution.
Parallel streaming performs faster because it executes using multiple cores. Sequential streams use only one core, so performance is slower.
The above is the detailed content of Difference between Parallel and Sequential Streams in Java. For more information, please follow other related articles on the PHP Chinese website!