该类CompletableFuture
允许添加在调用后未来完成时调用的操作complete(...)
.
我可以使用whenComplete(...)
来添加多个BiConsumer动作完成以后,并且当所有的人都在执行时执行complete(...)
方法被调用?
是的,添加了所有BiConsumer操作,并在调用完成(...)时以反向添加顺序执行.
演示这个的示例可能如下所示:
public class Application { public static void main(String[] args) { System.out.println("My tests ..."); CompletableFuturefutureString = new CompletableFuture (); futureString.whenComplete((s,e)->System.out.println("one " + s)); futureString.whenComplete((s,e)->System.out.println("two " + s)); futureString.whenComplete((s,e)->System.out.println("three " + s)); System.out.println("do something else; "+ futureString.isDone()); futureString.complete("step(s)"); System.out.println("Done " + futureString.isDone()); } }
运行此程序时,打印结果如下所示:
My tests ... do something else; false three step(s) two step(s) one step(s) Done true