Stream parallel() / sequential() question.
Boaz Nahum
boaznahum at gmail.com
Thu Apr 4 06:31:57 PDT 2013
Sory for my previouse mail, was too eraly
-------------------------------------------------------
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);
> }
>
> }
>
>
And then I saw that all consumer run in single thread if 'sequential()'
was the last to invoked, or all run in multiple threads if 'parallel()' was
last to be invoked.
Till now I believed that I I have non thread safe consumer I can just
project it by:
stream.sequential().peek(nonThreadSafeConsumer).parallel() ... other
concurrent threads safe work
Is my observation is wrong ? Or 'peek' have a special behavior ?
Thanks
and sorry again for the junk mail I sent.
Boaz
More information about the lambda-dev
mailing list