CompletableFuture.allOf that doens't return Void(CompletableFuture.allOf不能返回Void的解决方法)
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的解决方法)的更多相关文章
- jquery ajax 不执行赋值,return没有返回值的解决方法
大家先看一段简单的jquery ajax 返回值的js 复制代码 代码如下: function getReturnAjax{ $.ajax({ type:"POST", url:& ...
- 符号(void *)何解?符号(void **)又何解??
http://bbs.csdn.net/topics/70050852 对于多级指针或者数组,要掌握正确的识别方法:void* 是说: 这是一个指针,去掉一个(*)就是它所指向的,在这里是指向放vo ...
- 通过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 ...
- c++趣味之返回void
void a(){} void b(){return a();} int main() { b(); ; } 这个是能编译的(vs,gcc),void函数是能返回,一般不会这么写,但是这样确实可以.你 ...
- ((void *) 0)的含义和void的一些细节
一.在c语言中,0是一个特殊的值,它可以表示:整型数值0,空字符,逻辑假(false).表示的东西多了,有时候不好判断.尤其是空字符和数字0之间. 为了明确的指出,0是空字符的含义,用用到了: (() ...
- Java基础-接口.编写2个接口:InterfaceA和InterfaceB;在接口InterfaceA中有个方法void printCapitalLetter();在接口InterfaceB中有个方法void printLowercaseLetter();然 后写一个类Print实现接口InterfaceA和InterfaceB,要求 方法 实现输出大写英文字母表的功能,printLowerca
#34.编写2个接口:InterfaceA和InterfaceB:在接口InterfaceA中有个方法void printCapitalLetter():在接口InterfaceB中有个方法void ...
- STM32库函数void USART_SendData的缺陷和解决方法
void USART_SendData()函数在快速发送时存在问题 有丢数据的可能 转自https://blog.csdn.net/qq_27114397/article/details/506015 ...
- 无法解析的外部符号 "public: static void __cdecl std::_String_base::_Xran(void)" (?_Xran@_String_base@std@@SAXXZ)"
采用下面的方法.重新编译了一下依赖的库,OK了. 问题描述: 今天用VS2010编译一个在VS2008下Coding的工程的时候,VS给出了一堆链接错误信息,如下图所示: 在ErrorList里面 ...
- The return type is incompatible with JspSourceDependent.getDependants():JasperException问题分析与解决方法
Linux下基于JSP的报表集成到项目中后,显示不出来,查看tomcat的日志.有例如以下报错信息: The return type is incompatible with JspSourceDep ...
随机推荐
- Summer training #3
A:给一个包含字母 加号 括号的序列 要求你删除多余的括号然后输出 (待改) #include <bits/stdc++.h> #include <cstring> #incl ...
- 通过jenkins api远程调用job
curl http://jenkins地址/job/job_name/config.xml --user username:token
- bom操作,事件与jquery
BOM操作中 window关键字 可以不写 DOM操作 学习如何查找节点 如何查找标签 选择器 事件 当符合某个条件下 自动触发的动作/响应 js绑定事件的方式 方式1 不推荐使用 <butto ...
- php类知识---trait特性
#由于php类只支持单一继承,但我们又需要使用一些类的优秀特性,因此有了trait <?php trait cpc #trait 下的方法只能用public { function trainni ...
- Python之列表与元组的区别详解
相同点:都是序列类型 回答它们的区别之前,先来说说两者有什么相同之处.list 与 tuple 都是序列类型的容器对象,可以存放任何类型的数据.支持切片.迭代等操作 foos = [0, 1, 2, ...
- Codeforces 852F String Compression
题目 OvO http://codeforces.com/contest/825/problem/F 题解 KMP+DP 十分优雅地利用了KMP的fail数组 fail[k]表示第k个后缀的的fail ...
- jquery checked选择器 语法
jquery checked选择器 语法 作用::checked 选择器选取所有选中的复选框或单选按钮.直线电机参数 语法:$(":checked") jquery checked ...
- gdb插件gef安装爬坑
0x00: gdb是linux下的调试利器,但无奈界面不太友好,所以需要一些辅助插件. 0x01:关于插件选择 之前我一直使用的是pead,之前了解到还有个插件gef,因为gef支持多构架,而且hea ...
- 如何查询Office版本号
造冰箱的大熊猫@cnblogs 2019/1/28 如何查询当前所用Microsoft Office的版本信息? 以Word 2007为例,点击程序左上角的Office图标,在弹出的菜单中选择“Wo ...
- Confluence 6.15 使用附件宏
希望添加附件宏到一个页面中: 从编辑工具栏中,选择 插入(Insert) > 其他宏(Other Macros) 找到并且选择需要的宏. 可用使用自动完成加快这个过程:输入 { 然后开始输入 ...