[External] : Re: A new build and a new structured concurrency API
Remi Forax
forax at univ-mlv.fr
Thu Nov 18 07:37:37 UTC 2021
----- Original Message -----
> From: "Ron Pressler" <ron.pressler at oracle.com>
> To: "Alex Otenko" <oleksandr.otenko at gmail.com>
> Cc: "loom-dev" <loom-dev at openjdk.java.net>
> Sent: Jeudi 18 Novembre 2021 00:28:28
> Subject: Re: [External] : Re: A new build and a new structured concurrency API
>> On 17 Nov 2021, at 21:48, Alex Otenko <oleksandr.otenko at gmail.com> wrote:
>>
>> Thanks for the great update.
>>
>> I am trying to comprehend the implications of fork with onComplete.
>>
>> The docs say onComplete may not be invoked at all. What is being saved here,
>> compared to cancel() that guarantees onComplete will be invoked.
>>
>> The other thing that seems odd is that onComplete is entirely side-effecting: it
>> has no communication with the task that got completed (the task can't delegate
>> responsibility for anything), and no communication with the consumer of Future
>> (onComplete can't delegate responsibility for anything). Like, I borrow a
>> connection - who's going to return it and how? I'd expect someone to have one
>> last glance at the context of the Callable to clean up.
>>
>
> Completion handlers allows you to factor out policies for the common and simple
> cases where you need to collect results or shutdown the executor session based
> on the task’s success or failure. A call to shutdown indicates that the
> computation is done — either successfully or unsuccessfully — and so there’s no
> point in processing further results. In more complicated — and, we believe,
> much rarer — cases, like the connection example in the javadoc, the completion
> handler is, indeed, insufficient, and you’d want to do cleanup processing
> inside the task and possibly call shutdown directly (shutdown is the concurrent
> analogue to a break statement in sequential loop).
This remember me something which is to use a Stream of Future as an API to see the result of tasks.
The onComplete() push a result into the stream, a user can use short-circuit operations like findFirst() or limit(), the shutdown() is called when the stream is closed.
Something like
int sum;
try(var executor = StructuredExecutor.open()) {
var streamHandler = new StreamHandler<Integer>(executor);
streamHandler.fork(() -> 2);
streamHandler.fork(...);
try(Stream<Future<Integer>> futures = streamHandler.futureStream()) {
sum = futures
.mapToInt(future -> future.resultOrElseThrow(e -> new WebApplication(e)))
.sum();
} // executor.shutdown()
} // executor.close()
>
> — Ron
Rémi
More information about the loom-dev
mailing list