当前位置:网站首页>CompletableFuture接口核心方法介绍
CompletableFuture接口核心方法介绍
2022-08-04 10:00:00 【三井08】
CompletableFuture 可以看做是futuretask接口的升级版
获取对象,用这四个方法:
runAsync(Runnable runnable,Executor executor) ;
runAsync(Runnable runnable);
supplyAsync(Supplier<U> supplier);
supplyAsync(Supplier<U> supplier, Executor executor)Demo:
ExecutorService executorService = Executors.newFixedThreadPool(3);
/**
* 无返回值的构造
*/
CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {
System.out.println(Thread.currentThread().getName()+"无返回值");
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}, executorService);
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
/**
* 创建有返回值的
*/
CompletableFuture<String> uCompletableFuture = CompletableFuture.supplyAsync(() -> {
System.out.println(Thread.currentThread().getName() +"有返回值");
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
return "hello supplier";
}, executorService);
System.out.println(voidCompletableFuture.get());
System.out.println(uCompletableFuture.get());
executorService.shutdown();边栏推荐
- HCIP 第十八天
- 无代码平台多行文字入门教程
- js文字转语音播报
- XCTF-easy_Maze
- LVS+Keepalived群集部署
- Interview at 14:00 in the afternoon, I came out at 14:08 with my head down, asking too much...
- After four years of outsourcing, the autumn recruits finally landed
- 学习在php中将特大数字转成带有千/万/亿为单位的字符串
- 学习在php中分析switch与ifelse的执行效率
- 数据万象内容审核 — 共建安全互联网,专项开展“清朗”直播整治行动
猜你喜欢

使用ClickHouse分析COS的清单和访问日志

DOM简述
![[Punctuality Atomic STM32 Serial] Chapter 1 Learning Method of the Book Excerpted from [Punctuality Atomic] MiniPro STM32H750 Development Guide_V1.1](/img/99/23e3dcb426769a415e33f7ff51bbb8.png)
[Punctuality Atomic STM32 Serial] Chapter 1 Learning Method of the Book Excerpted from [Punctuality Atomic] MiniPro STM32H750 Development Guide_V1.1

Win11不识别蓝牙适配器的解决方法

渗透——信息收集

物体颜色的来源

rk3399-339 usb设备复合 总体流程

Interview at 14:00 in the afternoon, I came out at 14:08 with my head down, asking too much...

IDEA 自动导入的配置(Auto import)

PD 源码分析- Checker: region 健康卫士
随机推荐
Detailed Explanation of Addresses Delivered by DHCP on Routing/Layer 3 Switches [Huawei eNSP]
Four common methods of network attacks and their protection
浅聊偏函数
HCIP 第十八天
关于ARM2440中断源个数的一点想法[通俗易懂]
无代码平台数字入门教程
无代码平台附件上传入门教程
[Cloud Residency Co-Creation] HCSD Celebrity Live Streaming – Employment Guide
多媒体和物联网技术让版本“活”起来 129张黑胶唱片“百年留声”
canvas画图时的bug记录
TiFlash 源码阅读(五) DeltaTree 存储引擎设计及实现分析 - Part 2
【C补充】指向指针或函数的指针
什么是元宇宙?
无代码平台多行文字入门教程
HTB-Nibbles
DOM简述
The difference between Mysql application log time and system time is eight hours
用匿名函数定义函数_c语言最先执行的函数是
无线Mesh自组网方案,CV5200无线模组应用,支持高清数据远距离传输
菲沃泰科创板上市:市值123亿 宗坚赵静艳夫妇身价76亿