From the below code, I want to execute foo3
after foo2
is complete in foo1
. The method signature of foo1
cannot be changed. One way would be to move foo3
into foo2
, but that would be breaking separation of concerns.
@Override
CompletableFuture<Integer> foo1(String str) {
return CompletableFuture.supplyAsync(() -> foo2(str));
}
Integer foo2(String str) {
return 0;
}
void foo3(String str) {
System.out.println("Done");
}
Assuming you just want to call the foo3
after foo2
and return foo2
output, then you can do like this:
return CompletableFuture.supplyAsync(() -> {
Integer r = foo2(str);
foo3("testing");
return r;
}
);
You mean like
foo1(str).thenRun(() -> foo3(str);
since you don't seem interested in foo1
's result.
If foo3
call should be part of foo1
, then this is one way:
CompletableFuture<Integer> foo1(String str) {
return CompletableFuture.supplyAsync(() -> foo2(str))
.thenApply(foo2Result -> {
foo3(str);
return foo2Result;
}
);
}
Or if you want to call it, in places where foo1
is called:
foo1(str).thenApply(result -> {
foo3(str);
return result;
}
);
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.