So as it goes in the current scenario, we have a set of APIs as listed below:
Consumer start();
Consumer performDailyAggregates();
Consumer
Even if the Stream
is made parallel, the resulting compound Consumer
will execute the individual consumers in order, assuming:
The Stream
is ordered.
A stream sourced by a List
is ordered, even with parallel enabled.
The accumulator
passed to reduce()
is associative.
Consumer::andThen
is associative.
Let's say you have a list of 4 consumers [A, B, C, D]
. Normally, without parallel, the following would happen:
x = A.andThen(B);
x = x.andThen(C);
compound = x.andThen(D);
so that calling compound.apply()
would call A
, B
, C
, then D
in that order.
If you enable parallel, the stream framework might instead split that to be processed by 2 threads, [A, B]
by thread 1, and [C, D]
by thread 2.
That means the following will happen:
x = A.andThen(B);
y = C.andThen(D);
compound = x.andThen(y);
The result is that x
is applied first, which means A
then B
, then y
is applied, which means C
then D
.
So although the compound consumer is built like [[A, B], [C, D]]
instead of the left-associative [[[A, B], C], D]
, the 4 consumers are executed in order, all because Consumer::andThen
is associative.