[concurrency-interest] A CompletableFuture Question

Haim Yadid haim at performize-it.com
Tue Apr 29 23:16:51 EDT 2014


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20140430/1087bd61/attachment-0001.html>


More information about the Concurrency-interest mailing list