2 ms·
The individual steps in a java stream don’t run in separate threads/processes. So, for example. given the following code: IntStream.of(1,2,3,4,5,6,7,8,
by dhosek 4y ago
The individual steps in a java stream don’t run in separate threads/processes. So, for example. given the following code:
IntStream.of(1,2,3,4,5,6,7,8,9)
.peek(System.out::println)
.boxed()
.map(val -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
return "a" + val;
})
.peek(System.out::println)
.map(val -> {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
return "b" + val;
})
.forEach(System.out::println);
the forEach essentially acts as the driver for each step so that you’ll see everything happening in sequence. The equivalent shell script version of this would output in the order (approximately) of 1, 2, 3, 4, 5, 6, 7, 8, 9, a1, a2, a3, ab1, a4, a5, a6, ab2, a7, a8, a9, ab3, ab4, ab5, ab6, ab7, ab8, ab9 and finish in roughly 28 seconds instead of the 36 seconds that the streams version does. Streams are syntactic sugar and do not provide parallelism on their own.¹
⸻
1. And don’t get me started on the hot mess that’s the Java parallelStream
- krackers 4y agoBut don't pipes also block when the buffer (~1-4kb) is filled until the output can be consumed? So as I understand it's still sequential in the bash version, only there's a bit of buffering at play.