Edit: My question is different, it has no relevance to the linked question.
I've following code with completion handler.
FutureTask<Void> futureTask = new FutureTask<Void>(() -> {
System.out.println("callback");
return null;
});
Runnable task = () -> {
for(int i=0; i<5; i++) {
System.out.println(Thread.currentThread().getName() + " " + i);
}
futureTask.run();
};
new Thread(task).start();
new Thread(task).start();
Basically I'm looking for completion handler for variable number of tasks, or is there another approach?
I'm inspired from this answer but seems it's part of some library while I'm looking for a native solution.
Here's my attempt with completable futures with the result handler at the end.
public void test() {
CompletableFuture
.supplyAsync(() -> method1())
.supplyAsync(() -> method2())
.supplyAsync(() -> result());
}
public String method1() {
System.out.println("calling 1");
return "method1";
}
public String method2() {
System.out.println("calling 2");
return "method2";
}
public String result() {
System.out.println("result");
return "result";
}