简体   繁体   中英

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. See, for example, the documentation of 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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