So I have an interface SetupTask
that has only a single method run()
which returns a CompletableFuture
. I have a List<SetupTask>
. I want to run the first one and when that is complete run the second and when that is complete run the third and so on. Originally I tried CompletableFuture.allOf()
but that would run them all in parallel.
You could chain all these tasks one after another consistently calling .thenCompose. This method executes some function that returns a CompletableFuture
when the given stage is complete. I used CompletableFuture<Void>
just to demonstrate how it would look like.
CompletableFuture<Void> current = CompletableFuture.completedFuture(null);
for (SetupTask task : tasks) {
current = current.thenCompose(v -> task.run());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With