unrdered()

Brian Goetz brian.goetz at oracle.com
Thu Apr 4 07:18:32 PDT 2013


> Consider making forEach ordered by default, and relying on unordered()
> to disable this.

We did consider this, and it is weird that this the only terminal that 
has unordered as its behavior, but I think the current behavior is right.

If someone does:

   seqStream.forEach(action)

They will expect that the action is performed sequentially in the 
calling thread.  If they do:

   seqStream.parallel().forEach(action)

I believe they will (reasonably) expect the action to happen in parallel 
across threads.  Constraining to encounter order gives up the vast 
majority of the parallelism.  I think if we did this people would say 
"parallel streams don't work."


Separately, Paul quite correctly points out that back-propagating 
unordered from the terminal is a pain.  In:

   seqStream.parallel().distinct().forEach()

Here, since the forEach will be unordered, there's no point in doing the 
more expensive ordered processing for distinct.  This only shows up for 
parallel pipelines with stateful operations.  In that case, we can walk 
backwards injecting unordered, but have to stop when we hit a 
short-circuit operation.  (Though we could just omit this for now, its 
just an optimization.)


More information about the lambda-libs-spec-experts mailing list