Using BaseStream.isParallel() after calling terminal operation

Tagir F. Valeev amaembo at gmail.com
Sun Feb 7 08:45:35 UTC 2016


Hello!

Currently the isParallel() spec says [1]:

> Calling this method after invoking an terminal stream operation
> method may yield unpredictable results.

The spliterator() spec says [2]:

> This is a terminal operation.

As a consequence, we cannot legally call isParallel() after calling
spliterator(). However it's actually called in Stream.concat()
implementation [3] as well as in Stream.takeWhile()/dropWhile()
default implementations [4], [5]. The same is for primitive streams.
In all of these cases (especially in concat case) it's possible that
third-party Stream interface implementation is passed, which,
according to spec, can legally return garbage or even throw
IllegalStateException upon calling isParallel(). So to my
understanding, either spec or implementations should be corrected to
match each other.

My suggestion is to make exceptions for calling isParallel() after
spliterator() or iterator() terminal operation: isParallel() should
still return correct value. Though probably it should be additionally
specified what is considered as correct value in this case.

What do you think? Should we bother about such small thing?

With best regards,
Tagir Valeev

[1] http://download.java.net/jdk9/docs/api/java/util/stream/BaseStream.html#isParallel--
[2] http://download.java.net/jdk9/docs/api/java/util/stream/BaseStream.html#spliterator--
[3] http://hg.openjdk.java.net/jdk9/dev/jdk/file/fddcdea594f5/src/java.base/share/classes/java/util/stream/Stream.java#l1248
[4] http://hg.openjdk.java.net/jdk9/dev/jdk/file/fddcdea594f5/src/java.base/share/classes/java/util/stream/Stream.java#l542
[5] http://hg.openjdk.java.net/jdk9/dev/jdk/file/fddcdea594f5/src/java.base/share/classes/java/util/stream/Stream.java#l608




More information about the core-libs-dev mailing list