Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java completablefuture加入vs获取_Java_Java 8_Completable Future - Fatal编程技术网

Java completablefuture加入vs获取

Java completablefuture加入vs获取,java,java-8,completable-future,Java,Java 8,Completable Future,CompletableFuture.get()和CompletableFuture.join()之间有什么区别 以下是我的代码: List<String> process() { List<String> messages = Arrays.asList("Msg1", "Msg2", "Msg3", "Msg4", "Msg5", "Msg6"

CompletableFuture.get()
CompletableFuture.join()
之间有什么区别

以下是我的代码:

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());
}
List进程(){
列表消息=Arrays.asList(“Msg1”、“Msg2”、“Msg3”、“Msg4”、“Msg5”、“Msg6”、“Msg7”、“Msg8”、“Msg9”,
“Msg10”、“Msg11”、“Msg12”);
MessageService=newmessageservice();
ExecutorService executor=Executors.newFixedThreadPool(4);
List mapResult=新建ArrayList();
CompletableFuture[]fanoutRequestList=新的CompletableFuture[messages.size()];
整数计数=0;
用于(字符串消息:消息){
CompletableFuture=CompletableFuture
.SupplySync(()->messageService.sendNotification(msg),executor)。异常(例如->“错误”)
.thenAccept(mapResult::add);
fanoutRequestList[count++]=未来;
}
试一试{
CompletableFuture.allOf(fanoutRequestList.get();
//CompletableFuture.allOf(fanoutRequestList.join();
}捕获(中断异常|执行异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
返回mapResult.stream().filter(s->!s.equalsIgnoreCase(“错误”)).collect(Collectors.toList());
}

我尝试了这两种方法,但结果没有什么不同。

唯一的区别是方法如何抛出异常
get()
Future
接口中声明为

V get() throws InterruptedException, ExecutionException;
这些异常都是选中的异常,这意味着它们需要在代码中处理。正如您在代码中看到的,IDE中有一个自动代码生成器询问是否代表您创建try-catch块

try {
  CompletableFuture.allOf(fanoutRequestList).get() 
} catch (InterruptedException | ExecutionException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
}
join()
方法不会抛出选中的异常

public T join()
相反,它抛出未检查的CompletionException。因此,在使用discused
List process
函数时,您不需要try-catch块,而是可以完全利用
excellective()
方法

CompletableFuture<List<String>> cf = CompletableFuture
    .supplyAsync(this::process)
    .exceptionally(this::getFallbackListOfStrings) // Here you can catch e.g. {@code join}'s CompletionException
    .thenAccept(this::processFurther);
CompletableFuture cf=CompletableFuture
.SupplySync(此::进程)
.Exception(this::getFallbackListOfStrings)//在这里您可以捕获例如{@code join}的CompletionException
.然后接受(此::进一步处理);

您可以找到
get()
join()
实现要求您捕获已检查的异常。当您从
get()
更改为
join()
时,您应该注意到这一差异,因为您将立即得到一个编译器错误,即
InterruptedException
ExecutionException
都不会被抛出
try
块。@holi java:
join()
不能被中断。@Holger是的,先生。我发现我不能中断任务。嗯,
get
存在,因为
CompletableFuture
实现了要求它的
Future
接口
join()
很可能已经引入,以避免在组合未来时需要捕获lambda表达式中的已检查异常。在所有其他用例中,可以随意使用您喜欢的任何东西。在线程上使用join或get作为这两个块真的有意义吗。我们不能通过使用其他组合方法来创建异步函数链来实现异步。当然,这取决于功能。但在spring中的一个服务方法被返回CompletableFuture的控制器方法调用的情况下,根本不调用get或join服务方法更有意义,是吗?