19

I have 3 CompletableFutures all 3 returning different data types.

I am looking to create a result object that is a composition of the result returned by all the 3 futures.

So my current working code looks like this:

public ClassD getResultClassD() {

    ClassD resultClass = new ClassD();
    CompletableFuture<ClassA> classAFuture = CompletableFuture.supplyAsync(() -> service.getClassA() );
    CompletableFuture<ClassB> classBFuture = CompletableFuture.supplyAsync(() -> service.getClassB() );
    CompletableFuture<ClassC> classCFuture = CompletableFuture.supplyAsync(() -> service.getClassC() );

    CompletableFuture.allOf(classAFuture, classBFuture, classCFuture)
                     .thenAcceptAsync(it -> {
                        ClassA classA = classAFuture.join();
                        if (classA != null) {
                            resultClass.setClassA(classA);
                        }

                        ClassB classB = classBFuture.join();
                        if (classB != null) {
                            resultClass.setClassB(classB);
                        }

                        ClassC classC = classCFuture.join();
                        if (classC != null) {
                            resultClass.setClassC(classC);
                        }

                     });

    return resultClass;
}

My questions are:

  1. My assumption here is that since I am using allOf and thenAcceptAsync this call will be non blocking. Is my understanding right ?

  2. Is this the right way to deal with multiple futures returning different result types ?

  3. Is it right to construct ClassD object within thenAcceptAsync ?

  4. Is it appropriate to use the join or getNow method in the thenAcceptAsync lambda ?
Anand Sunderraman
  • 7,900
  • 31
  • 90
  • 150

4 Answers4

20

Your attempt is going into the right direction, but not correct. Your method getResultClassD() returns an already instantiated object of type ClassD on which an arbitrary thread will call modifying methods, without the caller of getResultClassD() noticing. This can cause race conditions, if the modifying methods are not thread safe on their own, further, the caller will never know, when the ClassD instance is actually ready for use.

A correct solution would be:

public CompletableFuture<ClassD> getResultClassD() {

    CompletableFuture<ClassA> classAFuture
        = CompletableFuture.supplyAsync(() -> service.getClassA() );
    CompletableFuture<ClassB> classBFuture
        = CompletableFuture.supplyAsync(() -> service.getClassB() );
    CompletableFuture<ClassC> classCFuture
        = CompletableFuture.supplyAsync(() -> service.getClassC() );

    return CompletableFuture.allOf(classAFuture, classBFuture, classCFuture)
         .thenApplyAsync(dummy -> {
            ClassD resultClass = new ClassD();

            ClassA classA = classAFuture.join();
            if (classA != null) {
                resultClass.setClassA(classA);
            }

            ClassB classB = classBFuture.join();
            if (classB != null) {
                resultClass.setClassB(classB);
            }

            ClassC classC = classCFuture.join();
            if (classC != null) {
                resultClass.setClassC(classC);
            }

            return resultClass;
         });
}

Now, the caller of getResultClassD() can use the returned CompletableFuture to query the progress state or chain dependent actions or use join() to retrieve the result, once the operation is completed.

To address the other questions, yes, this operation is asynchronous and the use of join() within the lambda expressions is appropriate. join was exactly created because Future.get(), which is declared to throw checked exceptions, makes the use within these lambda expressions unnecessarily hard.

Note that the null tests are only useful, if these service.getClassX() can actually return null. If one of the service calls fails with an exception, the entire operation (represented by CompletableFuture<ClassD>) will complete exceptionally.

Holger
  • 285,553
  • 42
  • 434
  • 765
  • thanks for the detailed reply. My only followup to your answer is that thenApplyAsync has a return type of CompletableFuture, how would this work here and how would one invoke this method and consume the result – Anand Sunderraman Feb 27 '17 at 15:59
  • 4
    No, it’s the return type of `allOf` which is `CompletableFuture`, that’s why the function passed `thenApplyAsync` receives `Void` as input (the `dummy` parameter above, instead of `dummy ->`, you could also write `(Void dummy) ->`). Then, the function translates the `Void` input (by actually ignoring it) to a `ClassD` result, so the result of `thenApplyAsync` will be `CompletableFuture`. – Holger Feb 27 '17 at 16:02
  • 1
    @Holger I was going down a similar route to you but I was using Optional.ofNullable in the service calls, so you can have `cCFuture.join().ifPresent(class::SetStuff)` – Ash Feb 27 '17 at 16:07
  • 1
    @Ash: there’s almost always more than one way to do it… – Holger Feb 27 '17 at 16:12
  • @Holger thanks for the explanation about the dummy parameter. Makes sense – Anand Sunderraman Feb 27 '17 at 16:25
6

I was going down a similar route to what @Holger was doing in his answer, but wrapping the Service Calls in an Optional, which leads to cleaner code in the thenApplyAsync stage

CompletableFuture<Optional<ClassA>> classAFuture
    = CompletableFuture.supplyAsync(() -> Optional.ofNullable(service.getClassA())));

CompletableFuture<Optional<ClassB>> classBFuture
    = CompletableFuture.supplyAsync(() -> Optional.ofNullable(service.getClassB()));

CompletableFuture<Optional<ClassC>> classCFuture
    = CompletableFuture.supplyAsync(() -> Optional.ofNullable(service.getClassC()));

return CompletableFuture.allOf(classAFuture, classBFuture, classCFuture)
     .thenApplyAsync(dummy -> {
        ClassD resultClass = new ClassD();

        classAFuture.join().ifPresent(resultClass::setClassA)
        classBFuture.join().ifPresent(resultClass::setClassB)
        classCFuture.join().ifPresent(resultClass::setClassC)

        return resultClass;
     });
Ash
  • 2,562
  • 11
  • 31
3

I ran into something similar before and created a short demo to show how I solved this issue.

Similar concept to @Holger except I used a function to combine each individual future.

https://github.com/te21wals/CompletableFuturesDemo

Essentially:

    public class CombindFunctionImpl implement CombindFunction {
    public ABCData combind (ClassA a, ClassB b, ClassC c) {
        return new ABCData(a, b, c);
   }
}

...

    public class FutureProvider {
public CompletableFuture<ClassA> retrieveClassA() {
    return CompletableFuture.supplyAsync(() -> {
        try {
            Thread.sleep(1000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return new ClassA();
    });
}

public CompletableFuture<ClassB> retrieveClassB() {
    return CompletableFuture.supplyAsync(() -> {
        try {
            Thread.sleep(2000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return new ClassB();
    });
}
public CompletableFuture<ClassC> retrieveClassC() {
    return CompletableFuture.supplyAsync(() -> {
        try {
            Thread.sleep(3000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return new ClassC();
    });
}
}

......

public static void main (String[] args){
    CompletableFuture<ClassA> classAfuture = futureProvider.retrieveClassA();
    CompletableFuture<ClassB> classBfuture = futureProvider.retrieveClassB();
    CompletableFuture<ClassC> classCfuture = futureProvider.retrieveClassC();

    System.out.println("starting completable futures ...");
    long startTime = System.nanoTime();

    ABCData ABCData = CompletableFuture.allOf(classAfuture, classBfuture, classCfuture)
            .thenApplyAsync(ignored ->
                    combineFunction.combind(
                            classAfuture.join(),
                            classBfuture.join(),
                            classCfuture.join())
            ).join();

    long endTime = System.nanoTime();
    long duration = (endTime - startTime);
    System.out.println("completable futures are complete...");
    System.out.println("duration:\t" + Duration.ofNanos(duration).toString());
    System.out.println("result:\t" + ABCData);
}
1

Another way to handle this if you don't want to declare as many variables is to use thenCombine or thenCombineAsync to chain your futures together.

public CompletableFuture<ClassD> getResultClassD()
{
  return CompletableFuture.supplyAsync(ClassD::new)
    .thenCombine(CompletableFuture.supplyAsync(service::getClassA), (d, a) -> {
      d.setClassA(a);
      return d;
    })
    .thenCombine(CompletableFuture.supplyAsync(service::getClassB), (d, b) -> {
      d.setClassB(b);
      return d;
    })
    .thenCombine(CompletableFuture.supplyAsync(service::getClassC), (d, c) -> {
      d.setClassC(c);
      return d;
    });
}

The getters will still be fired off asynchronously and the results executed in order. It's basically another syntax option to get the same result.

Adam R
  • 366
  • 3
  • 5
  • 1
    That's probably a good approach if there are only 2 or 3 futures to combine. However you should probably start from a `CompletableFuture.completedFuture(new ClassD())` as the instantiation is probably not worth running asynchronously. In fact, you could even instantiate it in a `thenApply()` on the first future. – Didier L Mar 07 '17 at 16:35