Stream parallel() / sequential() question.

Peter Levart peter.levart at gmail.com
Thu Apr 4 13:20:30 PDT 2013


On 04/04/2013 04:05 PM, Brian Goetz wrote:
> This was simplified recently.
>
> There is *one* sequential/parallel bit for the whole pipeline.  The
> stream starts off with it set one way or the other.  These calls
> overwrite it.  The bit is only acted on when you actually start the
> computation (invoke the terminal operation.)

Hi Brian,

So why are sequential()/parallel() methods needed then after all? We 
have the Collection.stream() and Collection.parallelStream() where the 
shape is pre-determined at the begining by the user. Is there an API 
that constructs a Stream where the shape is chosen automatically?

Regards, Peter

> On 4/4/2013 9:21 AM, Boaz Nahum wrote:
>> When I invoked parallel() or sequential() how backward it goes ?
>>
>> Let me explain, I wrote a simple Consumer that report how many different
>> threads used to run it:
>>
>> source.
>>               parallel().peek(new ThreadReporter("Segement 1 parallel")).
>>               sequential().peek(new ThreadReporter("Segement 2 sequential")).
>>               parallel().peek(new ThreadReporter("Segement 3 parallel")).
>>               sequential().peek(new ThreadReporter("Segement 4
>> sequential")).forEach((t) -> {});
>>
>>
>>
>>    private static class ThreadReporter implements Consumer<Integer> {
>>
>>
>>           @Override
>>           public void accept(Integer integer) {
>>               threads.put(Thread.currentThread(), true);
>>           }
>>
>>           public void report() {
>>               System.out.println("Name +'" + name + "': " + threads.size() +
>> " Thread(s)");
>>           }
>>       }
>>



More information about the lambda-dev mailing list