0

As per Oracle documentation :

invokeAll() : Executes the given tasks, returning a list of Futures holding their status and results when all complete. Future.isDone() is true for each element of the returned list. Note that a completed task could have terminated either normally or by throwing an exception. The results of this method are undefined if the given collection is modified while this operation is in progress.

CompletableFuture also implements Future with the following policies:

  • Since (unlike FutureTask) this class has no direct control over the computation that causes it to be completed, cancellation is treated as just another form of exceptional completion. Method cancel has the same effect as completeExceptionally(new CancellationException()). Method isCompletedExceptionally() can be used to determine if a CompletableFuture completed in any exceptional fashion.

  • In case of exceptional completion with a CompletionException, methods get() and get(long, TimeUnit) throw an ExecutionException with the same cause as held in the corresponding CompletionException. To simplify usage in most contexts, this class also defines methods join() and getNow(T) that instead throw the CompletionException directly in these cases.

What are the differences between

  • invokeAll() with Future

  • CompletableFuture

Since JDK 1.7 does not support CompletableFuture, can the same result will be achieved with invokeAll() with Future?

Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
  • Thanks for the feedback. Corrected the question. – Ravindra babu Aug 11 '15 at 15:37
  • `CompletableFuture` simply provides the possibility to act on the result asynchronously. `Future` does not. – Sotirios Delimanolis Aug 11 '15 at 15:42
  • 1
    I have checked the question now. That question converts 1.7 code to 1.8 compatible code. I am looking for achieving 1.8 feature with 1.7 code as I cannot upgrade JDK to 1.8 – Ravindra babu Aug 11 '15 at 15:45
  • Sorry, misread. The simple answer is: no. You'll need something similar to `CompletableFuture` for that added behavior. Guava provides `ListenableFuture`. The conversion is the same as in that previous post. See [here](http://stackoverflow.com/questions/30159229/how-to-convert-java-futurev-to-guava-listenablefuturev). – Sotirios Delimanolis Aug 11 '15 at 15:47

1 Answers1

1

Of course you can, if you write a bit of code:

  1. Copy/implement (the needed/related parts from) the CompletableFuture. For an example check this implementation on grepcode.
  2. Extend an ExecutorService (that you already use), and extend the protected method newTaskFor() responsible for instantiating Futures from a Runnable or a Callable, creating a new CompletableFuture() in it.
V G
  • 18,822
  • 6
  • 51
  • 89