1

I thought I understood the goal of CompletableFuture, introduced by Java8. Then I read this: https://medium.com/swlh/completablefuture-a-simplified-guide-to-async-programming-41cecb162308

In this article, the developer starts (or completes) a CompletableFuture INSIDE a new thread, which doesn't compute with me.

Can someone please explain, why anyone would benefit from having a separate Thread() complete a CompletableFuture?

 CompletableFuture<Double> futureResult = new CompletableFuture<>();
new Thread( ()-> {
    try{
    //some long process
    futureResult.complete(10.0);
    }catch(Exception e){
    futureResult.completeExceptionally(e);
   }
    }).start();
return futureResult;

Edit: As pointed out in the comments, and accepted answer, unsurprisingly, my confusion was rooted in my misunderstanding of CompletableFuture. In hindsight, it seems pretty obvious that, the new asynchronous activity would need to be started somewhere so why not in a new thread, since that new activity would be blocking?

Excellent, for gaining insight into CompletableFuture.

HellishHeat
  • 2,280
  • 4
  • 31
  • 37
  • I looked through the article, and can't find any example where a thread is started inside a Future. Can you please copy the code you are asking about? – marstran Aug 09 '21 at 10:03
  • Sorry for the confusion, I have updated the question. – HellishHeat Aug 09 '21 at 10:07
  • Sorry, that's better! – HellishHeat Aug 09 '21 at 10:11
  • You need the future to finish, so you have to start the task someway. You should probably use something other than just a thread. – matt Aug 09 '21 at 10:13
  • But I understood that a CompletableFuture is working on a separate Thread already. – HellishHeat Aug 09 '21 at 10:17
  • Right, so in this case the code is a completable future supplier. [CompelableFuture](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/concurrent/CompletableFuture.html) in their example, the future comes with an executor service and all. – matt Aug 09 '21 at 10:20
  • what "doesn't compute" with you, specifically? You can chain many dependent actions on a `CompletableFuture` (think conditionally for example) and only start those (calling `complete` for example) when you need/want to; as one example. – Eugene Aug 10 '21 at 01:23
  • Well, as far as I was concerned, a completableFuture was, by definition, asynchronous. I didn't understand the benefit of running that asynchronous part in a separate thread. – HellishHeat Aug 10 '21 at 06:54
  • as @matt pointed out, supplyAsync would achieve the same as a separate thread. – HellishHeat Aug 10 '21 at 06:59
  • 2
    This is simply the way, `CompletableFuture` works. You may use `supplyAsync` for exactly the same purpose, if it fits. But there might be scenarios where you want to use a certain already existing thread, for example, or you want to have more control over the operation. E.g., [handle checked exceptions](https://stackoverflow.com/a/28961083/2711488). The purpose of the linked article is to give you an overview of the existing alternatives. `runAsync` and `supplyAsync` are shown right beneath the example you’ve cited. – Holger Aug 10 '21 at 16:42

1 Answers1

4

A CompletableFuture implies that it is executing, so when you provide one you should have it started some how. Your example uses a Thread which doesn't give you much control.

If you don't care you should use CompletableFuture.completeAsync which would take care of the executor/thread.

In the provided code, the Thread is doing all of the work, and then 'completing' the future.

HellishHeat
  • 2,280
  • 4
  • 31
  • 37
matt
  • 10,892
  • 3
  • 22
  • 34