简体   繁体   English

Java 8 Streams,部分工作作为并行流,另一部分作为顺序流

[英]Java 8 Streams, perform part of the work as parallel stream and the other as sequential stream

I have a stream performing a series of operations in parallel, then I have to write the results into a file, so I need the writing operation to be sequential, but it needs to be performed as a stream, I have lots of data to write, I cannot use an intermediate collection.我有一个流并行执行一系列操作,然后我必须将结果写入文件,所以我需要写入操作是顺序的,但是它需要作为一个流来执行,我有很多数据要写入,我不能使用中间集合。 Is there a way to do that?有没有办法做到这一点?

I thought to a solution that doesn't seem very clean, that is to make the writing method synchronized.我想到了一个看起来不太干净的解决方案,那就是让写法同步。 Is this approach the only possible?这种方法是唯一可能的吗? is there some other way?还有其他方法吗?

Thank you.谢谢你。

There is no need to turn a Stream to sequential in order to perform a sequential terminal operation.无需将Stream顺序来执行顺序终端操作。 See, for example, the documentation of Stream.forEachOrdered :例如,参见Stream.forEachOrdered的文档:

This operation processes the elements one at a time, in encounter order if one exists.此操作一次处理一个元素,如果存在,则按遇到顺序处理。 Performing the action for one element happens-before performing the action for subsequent elements, but for any given element, the action may be performed in whatever thread the library chooses.对一个元素执行操作发生在对后续元素执行操作之前,但对于任何给定元素,该操作可以在库选择的任何线程中执行。

In other words, the action may be called by different threads as a result of the parallel processing of the previous steps, but it is guaranteed to be thread safe, does not call the action concurrently and even maintains the order if the stream was ordered .换句话说,由于前面步骤的并行处理,动作可能会被不同的线程调用,但它保证是线程安全的,不会并发调用动作,如果流被排序,甚至会保持顺序。

So there is no need to do any additional synchronization if you use forEachOrdered to specify the write to a file as terminal operations.因此,如果您使用forEachOrdered将写入文件指定为终端操作,则无需进行任何额外的同步。

Similar guarantees apply to other terminal operations regarding certain functions.类似的保证适用于有关某些功能的其他终端操作。 It's critical to study the documentation of the specific operation and to understand which guarantees are made and what is left unspecified.研究特定操作的文档并了解做出了哪些保证以及未指定的内容至关重要。

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

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