Java 8 Streams, perform part of the work as parallel stream and the other as sequential stream -
i have stream performing series of operations in parallel, have write results file, need writing operation sequential, needs performed stream, have lots of data write, cannot use intermediate collection. there way that?
i thought solution doesn't seem clean, make writing method synchronized. approach possible? there other way?
thank you.
there no need turn stream
sequential in order perform sequential terminal operation. see, example, documentation of stream.foreachordered
:
this operation processes elements 1 @ time, in encounter order if 1 exists. performing action 1 element happens-before performing action subsequent elements, given element, action may performed in whatever thread library chooses.
in other words, action may called different threads result of parallel processing of previous steps, guaranteed thread safe, not call action concurrently , maintains order if stream ordered.
so there no need additional synchronization if use foreachordered
specify write file terminal operations.
similar guarantees apply other terminal operations regarding functions. it’s critical study documentation of specific operation , understand guarantees made , left unspecified.
Comments
Post a Comment