[b84 regression?] stream.parallel()/sequential() return the stream itself when they are not expected to
Brian Goetz
brian.goetz at oracle.com
Fri Apr 5 09:22:55 PDT 2013
That's right. With the new and simplified view of sequential/parallel
-- that it is an all-or-nothing property of the entire pipeline that
takes effect when the pipeline is evaluated -- the interpretation of
isParallel is now "if I were to run this pipeline, would I get a serial
or parallel execution".
Implementing it this way -- with mutation rather than with linking a new
head onto the pipeline -- is a compromise between adhering to strict
functional semantics and implementation efficiency.
On 4/5/2013 12:09 PM, Dmitry Bessonov wrote:
> Currently in the b84 implementation
> calling sequential() on a parallel stream results in returning the
> stream itself.
>
> Moreover after calling sequential() on the parallel stream the original
> stream appears to be parallel no more:
>
> Stream<String> stream = Arrays.asList("a", "b",
> "c").parallelStream();
> System.out.println(stream);
> System.out.println(stream.isParallel());
> System.out.println(stream.sequential());
> System.out.println(stream.isParallel());
>
> the output will be
>
> java.util.stream.ReferencePipeline$Head at 1f3c5b5
> true
> java.util.stream.ReferencePipeline$Head at 1f3c5b5
> false
>
> -Dmitry
>
>
> On 05.04.2013 19:46, Brian Goetz wrote:
>> Here is what the spec currently says:
>>
>> /**
>> * Produces an equivalent stream that is parallel.
>> * If this stream is already parallel, may return itself.
>> *
>> * <p>This is an <a
>> href="package-summary.html#StreamOps">intermediate operation</a>.
>> *
>> * @return a parallel stream
>> */
>>
>> I believe this is what you are asking for? Sequential now says the
>> same thing (as will unordered() in a few minutes.)
>>
>> On 4/5/2013 11:40 AM, Dmitry Bessonov wrote:
>>> Consider the following code sample:
>>>
>>> Stream<String> stream = Arrays.asList("a", "b", "c").stream();
>>> out.println("stream = " + stream);
>>> out.println("stream.isParallel() = " + stream.isParallel());
>>> out.println("stream.parallel() = " + stream.parallel());
>>> out.println("stream.parallel() = " + stream.parallel());
>>> out.println("stream.parallel().isParallel() = " +
>>> stream.parallel().isParallel());
>>>
>>>
>>> With b83 a
>>> "java.lang.IllegalStateException: Stream is already linked to a child
>>> stream"
>>> is thrown on attempt to double link a stream.
>>>
>>>
>>> With b84 the output is quite unexpected:
>>>
>>> stream = java.util.stream.ReferencePipeline$Head at 1f3c5b5
>>> stream.isParallel() = false
>>> stream.parallel() = java.util.stream.ReferencePipeline$Head at 1f3c5b5
>>> stream.parallel() = java.util.stream.ReferencePipeline$Head at 1f3c5b5
>>> stream.parallel().isParallel() = true
>>>
>>>
>>> If it's not a regression
>>> then it looks like the spec
>>> for java.util.stream.BaseStream.parallel() deserves to be updated.
>>>
>>>
>>> The same issue takes place with parallel->sequential
>>> stream transformation so the spec for sequential() needs an update too.
>>>
>>> -Dmitry
>>>
>>>
>>>
>
More information about the lambda-dev
mailing list