Remark on the StructuredTaskScope API of Java 25

Alan Bateman alan.bateman at oracle.com
Sun Oct 19 10:32:09 UTC 2025


On 25/09/2025 21:46, forax at univ-mlv.fr wrote:
>
>
> ------------------------------------------------------------------------
>
>     *From: *"Alan Bateman" <alan.bateman at oracle.com>
>     *To: *"Remi Forax" <forax at univ-mlv.fr>, "loom-dev"
>     <loom-dev at openjdk.java.net>
>     *Sent: *Thursday, September 25, 2025 3:50:16 PM
>     *Subject: *Re: Remark on the StructuredTaskScope API of Java 25
>
>     On 24/09/2025 16:37, Remi Forax wrote:
>
>         :
>
>            - allSuccessfulOrThrow() should return a Joiner<T, List<Subtask<T>>>, so the result is a List and not a stream.
>              In terms of implementation, in result(), the code should be
>                return Collections.unmodifiableList(subtasks);
>
>     Can you say more on this? Right now, preferring a stream works
>     well. For a Joiner returned by allSuccessfulOrThrow it makes it
>     easy to use join().map(Subtask::get) or other mapping function.
>     Add .toList() to get a list. 
>
>
> A collection is more powerful than a Stream, you can always do more 
> with a List than with a Stream
> (like indexed access).
>
> Yes, you can always call toList() on a stream, but you are asking to 
> duplicate all the elements,
> here stream.toList() is semantically equivalent to a call to 
> List.copyOf(), so it's slow if you have quite a lot of elements.
>
> So yes, it might be convenient for some use cases to return a stream 
> than to call .stream() on the returned List,
> but you are trading convenience for performance.

I don't think this is right argument to change it to return List. 
However, another angle is that users might assume the stream is lazily 
populated and that results can be consumed before join completes. For 
this Joiner, join is meant to wait until all subtasks complete or any 
subtask fails. So while returning a Stream is much more flexible, it may 
indeed be better to return a List<T>.

-Alan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/loom-dev/attachments/20251019/48637237/attachment.htm>


More information about the loom-dev mailing list