[concurrency-interest] A CompletableFuture Question

Haim Yadid haim at performize-it.com
Wed Apr 30 02:10:16 EDT 2014


And ofcourse it doesn't work :)
Hope this one will be better:

    static<T> CompletableFuture<T>
holdDownExceptionally(CompletableFuture<T>f, CountDownLatch latch) {
        return f.exceptionally((t) -> {
                try {
                    latch.await();
                } catch (Exception e) {
                    throw new RuntimeException(t);
                }
                throw new RuntimeException(t);
             }).thenApply((r) -> {latch.countDown();return r;});
    }

    static <T,U> CompletableFuture<U> myApplytoEither(CompletableFuture<T>
f1, CompletableFuture<T> f2,Function<? super T, U> fn)  {
        CountDownLatch sync = new CountDownLatch(1);
        CompletableFuture<T> f1be = holdDownExceptionally(f1,sync);
        CompletableFuture<T> f2be = holdDownExceptionally(f2,sync);
        return f1be.applyToEither(f2be,fn);
    }


On Wed, Apr 30, 2014 at 7:24 AM, Haim Yadid <haim at performize-it.com> wrote:

> Thanks Martin,
> I now understand (documentation can be improved though - a one liner to
> explain what the method do instead referencing another place).
>
> Going back to the essence.   I am interested in the following
> functionality : I do not care about "the other" future whether it is too
> late or exceptional.
> The only way I can think about making it work as I expect is by  slowing
> down the exceptional flow and then cancelling it when all is done.
>
>
>     static <T,U> CompletableFuture<U> myApplytoEither(CompletableFuture<T>
> f1, CompletableFuture<T> f2,Function<? super T, U> fn)  {
>         CompletableFuture<T> f1be = f1.exceptionally((t) ->
> {sleep(100000);throw new RuntimeException(t);});
>         CompletableFuture<T> f2be = f1.exceptionally((t) ->
> {sleep(100000);throw new RuntimeException(t);});
>         CompletableFuture t =
> CompletableFuture.allOf(f1,f2).handle((res,trw)->
> {f1be.cancel(true);f2be.cancel(true); return null;});
>         return f1be.applyToEither(f2be,fn);
>     }
>
> I doubt that it really work probably there is some reason for it to be
> flawed:)
>
>
> On Wed, Apr 30, 2014 at 6:37 AM, Martin Buchholz <martinrb at google.com>wrote:
>
>> 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
>>>
>>>
>>
>
>
> --
> 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/80bbfbaf/attachment.html>


More information about the Concurrency-interest mailing list