Java Completablefuture join vs get
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45490316/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
completablefuture join vs get
提问by Nomeswaran Veligaram
What is the difference between CompletableFuture.get() and CompletableFuture.join()
CompletableFuture.get() 和 CompletableFuture.join() 有什么区别
Below is the my code:
下面是我的代码:
List<String> process() {
List<String> messages = Arrays.asList("Msg1", "Msg2", "Msg3", "Msg4", "Msg5", "Msg6", "Msg7", "Msg8", "Msg9",
"Msg10", "Msg11", "Msg12");
MessageService messageService = new MessageService();
ExecutorService executor = Executors.newFixedThreadPool(4);
List<String> mapResult = new ArrayList<>();
CompletableFuture<?>[] fanoutRequestList = new CompletableFuture[messages.size()];
int count = 0;
for (String msg : messages) {
CompletableFuture<?> future = CompletableFuture
.supplyAsync(() -> messageService.sendNotification(msg), executor).exceptionally(ex -> "Error")
.thenAccept(mapResult::add);
fanoutRequestList[count++] = future;
}
try {
CompletableFuture.allOf(fanoutRequestList).get();
//CompletableFuture.allOf(fanoutRequestList).join();
} catch (InterruptedException | ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return mapResult.stream().filter(s -> !s.equalsIgnoreCase("Error")).collect(Collectors.toList());
}
I have tried with both methods but no difference in result.
我已经尝试过这两种方法,但结果没有区别。
Thanks
谢谢
回答by Dawid Wróblewski
the only difference is how methods throw exceptions. get()
is declared in Future
interface as
唯一的区别是方法如何抛出异常。get()
在Future
接口中声明为
V get() throws InterruptedException, ExecutionException;
The exceptions are both checkedexceptions which means they need to be handled in your code. As you can see in your code an automatic code generator in your IDE asked if to creat try-catch block on your behalf.
异常都是已检查的异常,这意味着它们需要在您的代码中进行处理。正如您在代码中看到的,IDE 中的自动代码生成器询问是否代表您创建 try-catch 块。
try {
CompletableFuture.allOf(fanoutRequestList).get()
} catch (InterruptedException | ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
The join()
method doesn't throw checkedexceptions.
该join()
方法不会抛出已检查的异常。
public T join()
Instead it throws uncheckedCompletionException. So you do not need a try-catch block and instead you can fully harness exceptionally()
method when using the disscused List<String> process
function
相反,它会抛出未经检查的CompletionException。因此,您不需要 try-catch 块,而是exceptionally()
在使用讨论的List<String> process
功能时可以完全利用方法
CompletableFuture<List<String>> cf = CompletableFuture
.supplyAsync(this::process)
.exceptionally(this::getFallbackListOfStrings) // Here you can catch e.g. {@code join}'s CompletionException
.thenAccept(this::processFurther);
You can find both get()
and join()
implementation here
你可以在这里找到get()
和join()
实现