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());
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.