[concurrency-interest] A CompletableFuture Question

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

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?

        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);


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

More information about the Concurrency-interest mailing list