[concurrency-interest] A CompletableFuture Question

Doug Lea dl at cs.oswego.edu
Tue Apr 29 20:12:46 EDT 2014


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
>



More information about the Concurrency-interest mailing list