当前位置:网站首页>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();边栏推荐
猜你喜欢

Since his 97, I roll but he...

leetcode动态规划系列(求路径篇)
![[Punctuality Atom STM32 Serial] Chapter 3 Development Environment Construction Excerpted from [Punctual Atom] MiniPro STM32H750 Development Guide_V1.1](/img/6f/c736a3404377961e92b3bd1b5ea90e.png)
[Punctuality Atom STM32 Serial] Chapter 3 Development Environment Construction Excerpted from [Punctual Atom] MiniPro STM32H750 Development Guide_V1.1

LVS+Keepalived群集部署

LeetCode中等题之旋转图像

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

Win11如何隐藏输入法悬浮窗?
![[Cloud Residency Co-Creation] HCSD Celebrity Live Streaming – Employment Guide](/img/50/86f0edaab8317e22c9ffdb2a2c6e93.png)
[Cloud Residency Co-Creation] HCSD Celebrity Live Streaming – Employment Guide

XCTF-easy_Maze

MindSpore:MindSpore GPU版本安装问题
随机推荐
请问同一个oracle cdc表,如果flink job重新提交,是会全量读取一遍源数据还是增量呢?
How Oracle for current library or certain library data on the same server number?
参数优化。
Anton Paar Anton Paar Density Meter Hydrometer Repair DMA35 Performance Parameters
nsq部署_andlua辅助源码
Could you please talk about how the website is accessed?[Interview questions in the web field]
学习使用php把stdClass Object转array的方法整理
HCIP 第十八天
无线Mesh自组网方案,CV5200无线模组应用,支持高清数据远距离传输
MindSpore:图算融合报错
IDEA启动热部署
[Punctuality Atom STM32 Serial] Chapter 4 STM32 First Experience Excerpted from [Punctual Atom] MiniPro STM32H750 Development Guide_V1.1
MindSpore:损失函数问题
MindSpore:MindSpore GPU版本安装问题
leetcode经典例题——49.字母异位词分组
gom登录器配置教程_谷歌浏览器如何使用谷歌搜索引擎
GBsae 8 c database using an error, how to do?
leetcode动态规划系列(求路径篇)
江西发布紧急通知:全面开展涉校涉生安全隐患大排查
ps如何换背景颜色,自学ps软件photoshop2022,3种不同的方式笔记记录