0

What I am trying to do is calculate the depth of a tree structure asynchronously, I will have the first level of the tree and I want to kick off an asynchronous thread to calculate the depth of each of the nodes separately.

During the course of the calculations there might obviously be a fork in the tree, at which point I want to kick of an additional thread to calculate that branch.

I have got this working pretty much however I need to do some finishing up logic when all of these futures have completed. But I am having trouble with the additional CompletableFutures that are spawned along the way.

What method would I use to save up all of the starting CompletableFutures + the ones that are dynamically created along the way and have a way to await all of them to complete before doing any additional logic.

A.E
  • 125
  • 3
  • 11
  • Look at https://stackoverflow.com/questions/33944471/forkjointask-vs-completablefuture This discusson may help you – Mateusz Stefaniak May 29 '18 at 14:21
  • 1
    I think it would be good to provide a simplified version of your code for better understanding. From your description it seems you just need the right combination of `allOf()` and `thenCompose()`. Moreover it is a bit unclear what's your strategy for spawning tasks/threads. – Didier L May 30 '18 at 12:42

2 Answers2

0

You can try to get a value from each CompletableFuture. If a value is available check for the next one. If it is not available the executions stops for the current value before checking the next one. You can do with a code similar to the following:

List<CompletableFuture<Integer>> myList = ...
for (CompletableFuture<Integer> c: myList) {
   Integer x = c.get();
   ...
}
// You arrive here only when all the CompletableFuture have returned a 
// value.
Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56
0

Hard to say without known the specific use case or some code but you could use an external AtomicInteger to keep the track of the deep length. Something like:

CompletableFuture<Whatever> doStuff(..., int deep) {
  if(getCurrentDeep() < deep) setDeep(deep);
  // ...
  CompletableFuture<Whatever> stuff1 = doStuff(..., deep + 1);
  CompletableFuture<Whatever> stuff2 = doStuff(..., deep + 1);

  return stuff1.thenCombine(stuff2, (s1, s2) -> /* merge stuff */ )
}

In the other hand, would be much better if you could use an Actor library as Akka or Vertx to follow this approach.

RoberMP
  • 1,306
  • 11
  • 22