I cannot achieve good parallelization of stream processing when the stream source is a Reader. Running the code below on a quad-core CPU I observe 3 cores being
The parallel execution of streams is based on a fork-join model. For ordered streams, the parallel execution only works, if the stream can be split into parts, strictly following one another. In general, that's not possible with streams generated by BufferedReader. However, in theory parallel execution should be possible for unordered streams:
BufferedReader reader = ...;
reader.lines().unordered().map(...);
I am not sure if the stream returned by BufferedReader supports this kind of parallel execution. A very simple alternative is to create an intermediate list:
BufferedReader reader = ...;
reader.lines().collect(toList()).parallelStream().map(...);
In this case, the parallel execution starts after all lines have been read. This might be a problem, if reading the lines takes a long time. In this case, I recommend using an ExecutorService for parallel execution instead of parallel streams:
ExecutorService executor = ...;
BufferedReader reader = ...;
reader.lines()
.map(line -> executor.submit(() -> ... line ...))
.collect(toList())
.stream()
.map(future -> future.get())
.map(...);