import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.Stream; public class Main { static CompletableFuture<List<?>> allOf(CompletableFuture<?>... cfs) {
return CompletableFuture.allOf(cfs)
.thenApply(ignore -> Stream.of(cfs)
.map(cf -> cf.join())
.collect(Collectors.toList()));
} public static void main(String[] args) { // we have 3 (or any number) of CompletableFutures
CompletableFuture<String> cf1 = CompletableFuture.supplyAsync(() -> {sleep(); return "HELLO";});
CompletableFuture<Integer> cf2 = CompletableFuture.supplyAsync(() -> {sleep(); return ;});
CompletableFuture<Double> cf3 = CompletableFuture.supplyAsync(() -> {sleep(); return 20d;}); CompletableFuture<List<?>> allOf = allOf(cf1, cf2, cf3); //we call the method we just created above // we can get the -already there - result either using then
// Get result using:
allOf.thenAccept(l -> l.forEach(System.out::println)); // or using CompletableFuture.join() (or CompletableFuture.get())
// OR (non-typesafe)
String s = (String) allOf.join().get();
Integer i = (Integer) allOf.join().get();
Double d = (Double) allOf.join().get();
System.out.println(s + ", " + i + ", "+ d); sleep(); // because default CompletableFuture Executor is a daemon-thread based executor
} private static void sleep(int seconds) {
try {
TimeUnit.SECONDS.sleep(seconds);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

摘录地址:http://m-hewedy.blogspot.com/2017/02/completablefutureallof-that-doenst.html

CompletableFuture.allOf that doens't return Void(CompletableFuture.allOf不能返回Void的解决方法)的更多相关文章

  1. jquery ajax 不执行赋值,return没有返回值的解决方法

    大家先看一段简单的jquery ajax 返回值的js 复制代码 代码如下: function getReturnAjax{ $.ajax({ type:"POST", url:& ...

  2. 符号(void *)何解?符号(void **)又何解??

    http://bbs.csdn.net/topics/70050852 对于多级指针或者数组,要掌握正确的识别方法:void*  是说: 这是一个指针,去掉一个(*)就是它所指向的,在这里是指向放vo ...

  3. 通过qsort(void * lineptr[], int left, int rifht, int (*comp)(void *, void *))解读指针函数和void指针

    原函数是<The C programint  language >5.11文本行排序的程序,如下: void qsort(void *v[], int left, int right, i ...

  4. c++趣味之返回void

    void a(){} void b(){return a();} int main() { b(); ; } 这个是能编译的(vs,gcc),void函数是能返回,一般不会这么写,但是这样确实可以.你 ...

  5. ((void *) 0)的含义和void的一些细节

    一.在c语言中,0是一个特殊的值,它可以表示:整型数值0,空字符,逻辑假(false).表示的东西多了,有时候不好判断.尤其是空字符和数字0之间. 为了明确的指出,0是空字符的含义,用用到了: (() ...

  6. Java基础-接口.编写2个接口:InterfaceA和InterfaceB;在接口InterfaceA中有个方法void printCapitalLetter();在接口InterfaceB中有个方法void printLowercaseLetter();然 后写一个类Print实现接口InterfaceA和InterfaceB,要求 方法 实现输出大写英文字母表的功能,printLowerca

    #34.编写2个接口:InterfaceA和InterfaceB:在接口InterfaceA中有个方法void printCapitalLetter():在接口InterfaceB中有个方法void ...

  7. STM32库函数void USART_SendData的缺陷和解决方法

    void USART_SendData()函数在快速发送时存在问题 有丢数据的可能 转自https://blog.csdn.net/qq_27114397/article/details/506015 ...

  8. 无法解析的外部符号 "public: static void __cdecl std::_String_base::_Xran(void)" (?_Xran@_String_base@std@@SAXXZ)"

    采用下面的方法.重新编译了一下依赖的库,OK了.   问题描述: 今天用VS2010编译一个在VS2008下Coding的工程的时候,VS给出了一堆链接错误信息,如下图所示: 在ErrorList里面 ...

  9. The return type is incompatible with JspSourceDependent.getDependants():JasperException问题分析与解决方法

    Linux下基于JSP的报表集成到项目中后,显示不出来,查看tomcat的日志.有例如以下报错信息: The return type is incompatible with JspSourceDep ...

随机推荐

  1. ubuntu下python3.6.5import tensorflow显示非法指令(核心已转储)

    1.版本 ubuntu版本为14.04 python为3.6.5 tensorflow为pip3安装的1.8.0版本 2.解决 删除原先的tensorflow:sudo pip3 uninstall ...

  2. CNN for NLP(2)

    参考链接: 卷积神经网络(CNN)在句子建模上的应用, 卷积神经网络CNN在自然语言处理中的应用, CNN在NLP中的应用.

  3. libusb bulk

    https://github.com/IzyaSoft/EasyUsb https://github.com/ztguang/libusb-usbip-bulktransfer/blob/master ...

  4. BZOJ4777 [Usaco2017 Open]Switch Grass[最小生成树+权值线段树套平衡树]

    标题解法是吓人的. 图上修改询问,不好用数据结构操作.尝试转化为树来维护.发现(不要问怎么发现的)最小生成树在这里比较行得通,因为最近异色点对一定是相邻的(很好想),所以只要看最短的一条两端连着异色点 ...

  5. FZU-1901-Period 2(KMP)

    链接: https://vjudge.net/problem/FZU-1901 题意: For each prefix with length P of a given string S,if S[i ...

  6. 【C#-批量插入数据到数据库】DataTable数据批量插入数据的库三种方法:SqlCommand.EcecuteNonQurery(),SqlDataAdapter.Update(DataTable) ,SqlBulkCopy.WriteToServer(Datatable)

    第一种方法:使用SqlCommand.EcecuteNonQurery()  效率最慢 第二种方法:使用SqlDataAdapter.Update(DataTable)   效率次之 第三种方法:使用 ...

  7. php有哪些cms框架

    内容管理系统或CMS是一个用于管理新闻的应用程序,用户可以从后台管理系统发布.编辑和删除文章.HTML 和其他脚本语言不需要操作CMS,尽管使用它们会增加更多优势.无疑php的cms框架是最多的,国内 ...

  8. MD5 加密 字符串

    //获取字符串的MD5码 public string CreateMD5Hash(string input) { // Use input string to calculate MD5 hash S ...

  9. luogu 1593 因子和 约数+线性筛

    等比数列那里忘判项数等于 $1$ 的情况了. Code: #include <cstdio> #include <vector> #include <algorithm& ...

  10. docker 卸载与安装

    卸载 Docker自17.03版本开始分为两个版本Docker CE和Docker EE: Docker CE:Docker Community Edition,即Docker社区版 Docker E ...