当前位置:网站首页>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();
边栏推荐
- XCTF-easy_Maze
- safe-point(safepoint 安全点) 和 safe-region(安全区域)「建议收藏」
- Acwing 3208. Z字形扫描 偏移量+扩展图
- 数据万象内容审核 — 共建安全互联网,专项开展“清朗”直播整治行动
- PD 源码分析- Checker: region 健康卫士
- 张朝阳对话俞敏洪:谈宇宙、谈焦虑、谈创业、谈退休、谈人生
- leetcode单调栈经典例题——最大矩形
- 请问下Flink SQL如何写hologres分区表?我想要每天一个分区
- Anton Paar Anton Paar Density Meter Hydrometer Repair DMA35 Performance Parameters
- 密码字典生成工具pydictor/john
猜你喜欢
MindSpore:【model_zoo】【resnet】尝试用THOR优化器运行时报cannot import name ‘THOR‘
[Punctuality Atom STM32 Serial] Chapter 2 STM32 Introduction Excerpted from [Punctual Atom] MiniPro STM32H750 Development Guide_V1.1
MindSpore:MindSpore GPU版本安装问题
黑马瑞吉外卖之员工账号的禁用和启用以及编辑修改
No module named 'flask_misaka' has been resolved [BUG solution]
MindSpore:【AIR模型导出】导出时提示源码中select_op参数类型转换失败
Win11不识别蓝牙适配器的解决方法
HTB-Nibbles
leetcode每天5题-Day06
【COS 加码福利】COS 用户实践有奖征文,等你来投稿!
随机推荐
张朝阳对话俞敏洪:谈宇宙、谈焦虑、谈创业、谈退休、谈人生
Could you please talk about how the website is accessed?[Interview questions in the web field]
LVS-DR集群部署
VSCode学习资料
Inheritance and the static keyword
常用的输入对象
MindSpore:【model_zoo】【resnet】尝试用THOR优化器运行时报cannot import name ‘THOR‘
gom登录器配置教程_谷歌浏览器如何使用谷歌搜索引擎
Mysql应用日志时间与系统时间相差八小时
被Win11安全中心误删除的文件怎么恢复?
[Punctuality Atom STM32 Serial] Chapter 4 STM32 First Experience Excerpted from [Punctual Atom] MiniPro STM32H750 Development Guide_V1.1
字符串相关题目
无代码平台多项选择入门教程
leetcode每天5题-Day06
MindSpore:model.train中的dataset_sink_mode该如何理解?
LVGL's multi-language conversion tool -- a good assistant for font settings
各位大佬,请问mysql数据的cdc,能指定存量数据同步的zone为utc 吗
[Punctuality Atom STM32 Serial] Chapter 2 STM32 Introduction Excerpted from [Punctual Atom] MiniPro STM32H750 Development Guide_V1.1
Since his 97, I roll but he...
Detailed Explanation of Addresses Delivered by DHCP on Routing/Layer 3 Switches [Huawei eNSP]