I have the following method using CompletableFuture
like this:
public AClass aMethod() {
CompletableFuture<SomeClassA> someClassAFuture =
CompletableFuture.supplyAsync(() -> someMethodThatReturnsA());
CompletableFuture<SomeClassB> someClassBFuture =
CompletableFuture.supplyAsync(() -> someMethodThatReturnsB());
CompletableFuture<SomeClassC> someClassCFuture =
CompletableFuture.supplyAsync(() -> someMethodThatReturnsC());
CompletableFuture.allOf(someClassAFuture, someClassBFuture, someClassCFuture).join();
return new AClass(someClassAFuture.join(), someClassBFuture.join(), someClassCFuture.join());
}
This code has a deadlock problem when T
threads simultaneously enter the method if there are less threads in the fork join pool than T * 3
(because none of the allOf
calls can complete, and they will not return to the pool the currently taken threads).
The only way I have found to solve this was to limit the simultaneous threads inside the method (using Spring's @Async
annotation with a thread executor) or increasing the threads in the fork join pool.
I would like some better solution where I can completely forgot about the thread pool size. How can I rewrite this using Reactor or Akka?