Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.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.SupplySync()的返回类型分配给对象?_Java_Concurrency_Java 8_Completable Future - Fatal编程技术网

Java 如何将completableFuture.SupplySync()的返回类型分配给对象?

Java 如何将completableFuture.SupplySync()的返回类型分配给对象?,java,concurrency,java-8,completable-future,Java,Concurrency,Java 8,Completable Future,我已经在foreach循环中定义了completableFuture.SupplySync,因此每个入口每个异步任务都会添加一个列表,我需要在completableFuture.SupplySync的所有异步任务添加列表之后获得最终列表。如何实现这一点 代码段: unporcessedList.forEach(entry -> { CompletableFuture<List<ChangeLog>> cf

我已经在foreach循环中定义了completableFuture.SupplySync,因此每个入口每个异步任务都会添加一个列表,我需要在completableFuture.SupplySync的所有异步任务添加列表之后获得最终列表。如何实现这一点

代码段:

    unporcessedList.forEach(entry -> {                       
    CompletableFuture<List<ChangeLog>> cf =  
    CompletableFuture.supplyAsync((Supplier<List<ChangeLog>>) () -> {                            
    mongoDBHelper.processInMongo(entry, getObject(entry, map),entryList);
    return entryList;
    }, executor); 
    });

您可以使用get方法阻止应用程序,直到将来完成。因此,请使用类似这样的方法:

// Block and get the result of the Future
Supplier<List<ChangeLog>> result = cf.get();
此处介绍了更多示例:


希望这能有所帮助。

您可以使用get方法阻止您的应用程序,直到将来完成。因此,请使用类似这样的方法:

// Block and get the result of the Future
Supplier<List<ChangeLog>> result = cf.get();
此处介绍了更多示例:

希望这有帮助。

非阻塞版本

一般示例:

    List<String> entries = new ArrayList<>(2);
    entries.add("first");
    entries.add("second");

    List<CompletableFuture<String>> completableFutures = entries.stream()
            .map((entry) -> {
                        return CompletableFuture.supplyAsync(() -> {
                            try {
                                Thread.sleep(new Random().nextInt(5000) + 500);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            return entry.concat(String.valueOf(entry.length()));
                        }).thenApply((e) -> new StringBuilder(e).reverse().toString());
                    }
            ).collect(Collectors.toList());

    CompletableFuture
            .allOf(completableFutures.toArray(new CompletableFuture[completableFutures.size()]))
            .thenApply((v) -> completableFutures.stream().map((cf) -> cf.join()))
            .get()
            .forEach(System.out::println);
你的情况:

    List<CompletableFuture<List<ChangeLog>>> completableFutures = unporcessedList.stream()
            .map((entry) -> {
                        return CompletableFuture.supplyAsync((Supplier<List<ChangeLog>>) () -> {
                            mongoDBHelper.processInMongo(entry, getObject(entry, map), entryList);
                            return entryList;
                        }, executor);
                    }
            ).collect(Collectors.toList());

    CompletableFuture
            .allOf(completableFutures.toArray(new CompletableFuture[completableFutures.size()]))
            .thenApply((v) -> completableFutures.stream().map((cf) -> cf.join()))
            .get()
            .forEach(System.out::println);
非阻塞版本

一般示例:

    List<String> entries = new ArrayList<>(2);
    entries.add("first");
    entries.add("second");

    List<CompletableFuture<String>> completableFutures = entries.stream()
            .map((entry) -> {
                        return CompletableFuture.supplyAsync(() -> {
                            try {
                                Thread.sleep(new Random().nextInt(5000) + 500);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            return entry.concat(String.valueOf(entry.length()));
                        }).thenApply((e) -> new StringBuilder(e).reverse().toString());
                    }
            ).collect(Collectors.toList());

    CompletableFuture
            .allOf(completableFutures.toArray(new CompletableFuture[completableFutures.size()]))
            .thenApply((v) -> completableFutures.stream().map((cf) -> cf.join()))
            .get()
            .forEach(System.out::println);
你的情况:

    List<CompletableFuture<List<ChangeLog>>> completableFutures = unporcessedList.stream()
            .map((entry) -> {
                        return CompletableFuture.supplyAsync((Supplier<List<ChangeLog>>) () -> {
                            mongoDBHelper.processInMongo(entry, getObject(entry, map), entryList);
                            return entryList;
                        }, executor);
                    }
            ).collect(Collectors.toList());

    CompletableFuture
            .allOf(completableFutures.toArray(new CompletableFuture[completableFutures.size()]))
            .thenApply((v) -> completableFutures.stream().map((cf) -> cf.join()))
            .get()
            .forEach(System.out::println);

谢谢@runningriot,cf.get返回结束值,但我无法将其分配给类型Supplier的结果,即使我尝试将cf.get转换为供应商类型,但得到了运行时转换异常。因此,我只编写了cf.get并能够在这个方法中返回list return type。谢谢@runningriot,cf.get返回结束值,但我无法将其分配给类型Supplier的结果,即使我尝试将cf.get转换为Supplier类型,但得到了运行时转换异常。所以我只编写了cf.get,并能够在这个方法返回类型中返回list。