[concurrency-interest] A CompletableFuture Question

Martin Buchholz martinrb at google.com
Tue Apr 29 23:37:30 EDT 2014


Haim,

The doc you cited also says,
See the CompletionStage documentation for rules covering exceptional
completion.
which leads to:

Two method forms support processing whether the triggering stage completed
normally or exceptionally: Method whenComplete allows injection of an
action regardless of outcome, otherwise preserving the outcome in its
completion. Method handle additionally allows the stage to compute a
replacement result that may enable further processing by other dependent
stages. In all other cases, if a stage's computation terminates abruptly
with an (unchecked) exception or error, then all dependent stages requiring
its completion complete exceptionally as well, with a CompletionException
holding the exception as its cause. If a stage is dependent on both of two
stages, and both complete exceptionally, then the CompletionException may
correspond to either one of these exceptions. If a stage is dependent on
either of two others, and only one of them completes exceptionally, no
guarantees are made about whether the dependent stage completes normally or
exceptionally. In the case of method whenComplete, when the supplied action
itself encounters an exception, then the stage exceptionally completes with
this exception if not already completed exceptionally.



On Tue, Apr 29, 2014 at 8:16 PM, Haim Yadid <haim at performize-it.com> wrote:

> Thanks Doug,
>
> I find the documentation a bit confusing:
>
> applyToEither<http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CompletableFuture.html#applyToEither-java.util.concurrent.CompletionStage-java.util.function.Function->
> (CompletionStage<http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CompletionStage.html><?
> extends T<http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CompletableFuture.html>
> > other, Function<http://docs.oracle.com/javase/8/docs/api/java/util/function/Function.html><?
> super T<http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CompletableFuture.html>
> ,U> fn)
> Returns a new CompletionStage that, when either this or the other given
> stage complete normally, is executed with the corresponding result as
> argument to the supplied function.
>
> In other places of the documentation (such as thenApply)  "completes
> normally" means successfully and not exceptionally.
>
> In addition is there a way to do what I wanted to do in the first place ?
> Query several services pick up the fastest non erroneous answer. If all are
> exceptional pick one error.
> I only need one successful
>
>
>
> On Wed, Apr 30, 2014 at 3:12 AM, Doug Lea <dl at cs.oswego.edu> wrote:
>
>> On 04/29/2014 04:03 PM, Haim Yadid wrote:
>>
>>>
>>> Hi,
>>> Take a look on the following code snippet.
>>> Two CompletableFutures f1 returns 42 f2 throws exception.
>>> If f2 ends before f1 an exception will be thrown on f3.get() .
>>> As f1 is the only future which completes normally I would expect that it
>>> will
>>> wait for f1 to complete and return its result. What am I missing?
>>>
>>
>> A CompletableFuture is complete when its action either returns or throws
>> an exception. If exceptional, then the exception is propagated
>> to dependents. There are several CompletableFuture methods for handling
>> and transforming exceptional outcomes.
>>
>>
>>>          CompletableFuture<Integer> f1 = supplyAsync(
>>>                  () -> {sleep(2300);return 42;});
>>>          CompletableFuture<Integer> f2 = supplyAsync(
>>>                  () -> {sleep(2200);if (true) throw new
>>> RuntimeException();return 43;});
>>>
>>>          CompletableFuture<Integer> f3 = f1.applyToEither(f2,(r) -> r *
>>> r);
>>>
>>>          System.out.println(f3.get());
>>>
>>
>> This should print an exception.
>>
>> -Doug
>>
>>
>>>
>>> PS - sleep is a Thread.sleep() wrapper when swallows check exceptions
>>> and throws
>>> runtime exception instead.
>>>
>>>
>>>
>>> --
>>> Haim Yadid | Performization Expert
>>> Performize-IT | t +972-54-7777132
>>> www.performize-it.com <http://www.performize-it.com>
>>>
>>>
>>> _______________________________________________
>>> Concurrency-interest mailing list
>>> Concurrency-interest at cs.oswego.edu
>>> http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>>>
>>>
>> _______________________________________________
>> Concurrency-interest mailing list
>> Concurrency-interest at cs.oswego.edu
>> http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>>
>
>
>
> --
> Haim Yadid | Performization Expert
> Performize-IT | t +972-54-7777132
> www.performize-it.com
>
>
>
> --
> Haim Yadid | Performization Expert
> Performize-IT | t +972-54-7777132
> www.performize-it.com
>
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20140429/7cd08224/attachment.html>


More information about the Concurrency-interest mailing list