我是 Vert.x 异步编程的新手。我想知道如何使用期货并行运行多个任务。(与使用 Java 的 callable 类似)。
以下是我所拥有的,但任务是按顺序执行的。我错过了什么?
CompositeFuture.all(
Future.future(h -> {
runTask("Future one");
}),
Future.future(h -> {
runTask("Future two");
})
).onComplete(ar ->{
if(ar.succeeded()){
System.out.println("All succeeded");
}
else {
System.out.println("At least one failed");
}
});
public static Future<Void> runTask(String m) {
System.out.println("running " + m);
System.out.println("30th fibonacci num is " + fib(30)); //Calling a method that calculates the nth fibonacci
System.out.println("completed running " + m);
return Future.<Void>succeededFuture();
}
Output:
running Future one
Fib of 30 is 102334155
completed running Future one
running Future two
Fib of 30 is 102334155
completed running Future two
Done
But the expected output should be:
running Future one
running Future two
Fib of 30 is 102334155
Fib of 30 is 102334155
completed running Future one
completed running Future two
Done
Done