Jae Bradley
Jae Bradley

Reputation: 229

Don't wait for CompletableFuture

Let's say I have some logic that I want to execute in a separate thread, and then continue with the remaining logic in my method.

If I don't care about the result of the the logic in the separate thread (could complete exceptionally, could complete with a result), can I simply execute the logic via CompletableFuture.runAsync? Just to reiterate, I do not need any information about the state or completion of the logic executed in the separate thread.

For example,

public void doSomething() {
  // Don't care about the result of doSomethingElse - only care that execution starts and will complete with some outcome
  CompletableFuture.runAsync(() -> doSomethingElse());
  // do more things synchronously
  return;
}

Upvotes: 1

Views: 1841

Answers (1)

davidalayachew
davidalayachew

Reputation: 1553

You can, but beware - if your program finishes running before this CompletableFuture completes, then the CompletableFuture might just get force shut down. This is important because a program that just stops executing halfway through can cause some elusive and difficult to debug errors. So it's actually 3 possible end states instead of 2.

  1. Complete successfully
  2. Complete exceptionally
  3. Stopped before completion with no error or notification unless you explicitly designed around this.

Upvotes: 1

Related Questions