CompletableFuture allOf 方法行为
CompletableFuture allOf method behavior
我有以下一段 java 代码:
CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> {
try {
Thread.sleep(3);
} catch (InterruptedException e) {
throw new IllegalStateException(e);
}
return "Result of Future 1";
});
CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
try {
Thread.sleep(3);
} catch (InterruptedException e) {
throw new IllegalStateException(e);
}
return "Result of Future 2";
});
CompletableFuture<String> future3 = CompletableFuture.supplyAsync(() -> {
try {
Thread.sleep(3);
} catch (InterruptedException e) {
throw new IllegalStateException(e);
}
return "Result of Future 3";
});
boolean isdone = CompletableFuture.allOf(future1, future2, future3).isDone();
if (isdone) {
System.out.println("Future result " + future1.get() + " | " + future2.get() + " | " + future3.get());
} else {
System.out.println("Futures are not ready");
}
当我 运行 这段代码时,它总是打印 "Futures are not ready"。我在这里使用 allOf 方法,它应该等待所有 futures 完成,但主线程不在这里等待并打印 else 部分。有人可以帮我了解这里出了什么问题吗?
I am using allOf method here which should wait for all the futures to get completed
那不是 allOf
所做的。它创建了一个新的 CompletableFuture
is completed when all of the given CompletableFutures complete
。但是,它不会等待新 CompletableFuture
完成。
这意味着您应该调用一些等待此 CompletableFuture
完成的方法,此时所有给定的 CompletableFuture
都保证完成。
例如:
CompletableFuture<Void> allof = CompletableFuture.allOf(future1, future2, future3);
allof.get();
if (allof.isDone ()) {
System.out.println("Future result " + future1.get() + " | " + future2.get() + " | " + future3.get());
} else {
System.out.println("Futures are not ready");
}
输出:
Future result Result of Future 1 | Result of Future 2 | Result of Future 3
我有以下一段 java 代码:
CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> {
try {
Thread.sleep(3);
} catch (InterruptedException e) {
throw new IllegalStateException(e);
}
return "Result of Future 1";
});
CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
try {
Thread.sleep(3);
} catch (InterruptedException e) {
throw new IllegalStateException(e);
}
return "Result of Future 2";
});
CompletableFuture<String> future3 = CompletableFuture.supplyAsync(() -> {
try {
Thread.sleep(3);
} catch (InterruptedException e) {
throw new IllegalStateException(e);
}
return "Result of Future 3";
});
boolean isdone = CompletableFuture.allOf(future1, future2, future3).isDone();
if (isdone) {
System.out.println("Future result " + future1.get() + " | " + future2.get() + " | " + future3.get());
} else {
System.out.println("Futures are not ready");
}
当我 运行 这段代码时,它总是打印 "Futures are not ready"。我在这里使用 allOf 方法,它应该等待所有 futures 完成,但主线程不在这里等待并打印 else 部分。有人可以帮我了解这里出了什么问题吗?
I am using allOf method here which should wait for all the futures to get completed
那不是 allOf
所做的。它创建了一个新的 CompletableFuture
is completed when all of the given CompletableFutures complete
。但是,它不会等待新 CompletableFuture
完成。
这意味着您应该调用一些等待此 CompletableFuture
完成的方法,此时所有给定的 CompletableFuture
都保证完成。
例如:
CompletableFuture<Void> allof = CompletableFuture.allOf(future1, future2, future3);
allof.get();
if (allof.isDone ()) {
System.out.println("Future result " + future1.get() + " | " + future2.get() + " | " + future3.get());
} else {
System.out.println("Futures are not ready");
}
输出:
Future result Result of Future 1 | Result of Future 2 | Result of Future 3