当前位置:网站首页>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();边栏推荐
- Could you please talk about how the website is accessed?[Interview questions in the web field]
- DOM简述
- matlab练习程序(多线段交点)
- No module named 'flask_misaka' has been resolved [BUG solution]
- Win11如何隐藏输入法悬浮窗?
- 架构设计杂谈
- leetcode二叉树系列(二)
- HCIP 第十八天
- Interview at 14:00 in the afternoon, I came out at 14:08 with my head down, asking too much...
- 云计算适合什么企业_当前全球云计算处于发展
猜你喜欢

XCTF-reverse-signin

罗克韦尔AB PLC RSLogix5000中定时器指令使用方法介绍
![Layer 3 Switch/Router OSPF Configuration Details [Huawei eNSP Experiment]](/img/28/1a7ad13a15287a4cb84aabf39202a4.png)
Layer 3 Switch/Router OSPF Configuration Details [Huawei eNSP Experiment]

ps如何换背景颜色,自学ps软件photoshop2022,3种不同的方式笔记记录

MindSpore:Batchnorm only support nchw input!

无代码平台数字入门教程

sqlilabs less-38~39

Win10电脑经常发出叮咚声音怎么关闭

HCIP 交换实验
![No module named 'flask_misaka' has been resolved [BUG solution]](/img/cc/e379d23a41330d2335dd192e16e821.png)
No module named 'flask_misaka' has been resolved [BUG solution]
随机推荐
渗透——信息收集
开源一夏 | 查询分页不只有limit,这四种分页方法值得掌握
Win11系统重装用什么好 一键重装Win11教程
VRRP + MSTP configuration, huawei eNSP experiment 】 【
Win10电脑经常发出叮咚声音怎么关闭
Producer and Consumer Problems in Concurrent Programming
Ansible deployment scripts - pro available without pit
[论文翻译] Unpaired Image-to-Image Translation using Adversarial Consistency Loss
[Punctuality Atom STM32 Serial] Chapter 2 STM32 Introduction Excerpted from [Punctual Atom] MiniPro STM32H750 Development Guide_V1.1
HTB-Sense
二叉树与堆
无代码平台描述文字入门教程
架构设计杂谈
DOM简述
加降息与BTC流动性事件策略研究
MindSpore:MindSpore GPU版本安装问题
Detailed explanation of switch link aggregation [Huawei eNSP]
TiFlash 源码阅读(五) DeltaTree 存储引擎设计及实现分析 - Part 2
Detailed explanation of telnet remote login aaa mode [Huawei eNSP]
关于技术学习的6个观点