当前位置:网站首页>Introduction to the core methods of the CompletableFuture interface
Introduction to the core methods of the CompletableFuture interface
2022-08-04 10:02:00 【Mitsui 08】
CompletableFuture can be seen as an upgraded version of the futuretask interface
To get the object, use these four methods:
runAsync(Runnable runnable,Executor executor) ;runAsync(Runnable runnable);supplyAsync(Supplier supplier);supplyAsync(Supplier supplier, Executor executor)
Demo:
ExecutorService executorService = Executors.newFixedThreadPool(3);/*** Construct with no return value*/CompletableFuture voidCompletableFuture = CompletableFuture.runAsync(() -> {System.out.println(Thread.currentThread().getName()+"No return value");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);}/*** create a return value*/CompletableFuture uCompletableFuture = CompletableFuture.supplyAsync(() -> {System.out.println(Thread.currentThread().getName() + "return value");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();
边栏推荐
- Detailed Explanation of Addresses Delivered by DHCP on Routing/Layer 3 Switches [Huawei eNSP]
- 云计算适合什么企业_当前全球云计算处于发展
- 函数防抖与函数节流
- Win11怎么进行左右键对调?
- MySQL binlog都有哪些模式?
- XCTF-reverse-signin
- Interview at 14:00 in the afternoon, I came out at 14:08 with my head down, asking too much...
- 为企业数字化转型提供服务_数字赋能企业转型
- 常用的输入对象
- Four common methods of network attacks and their protection
猜你喜欢
Techwiz OLED:OLED器件的发光效率
LVS+Keepalived群集部署
2022 Cloud Native Computing代表厂商 | 灵雀云第三次入选Gartner中国ICT技术成熟度曲线报告
Win11系统重装用什么好 一键重装Win11教程
有了这篇 Kubernetes 的介绍,它的原理秒懂!
LVS-DR集群部署
MindSpore:【AIR模型导出】导出时提示源码中select_op参数类型转换失败
No module named 'flask_misaka' has been resolved [BUG solution]
渗透——信息收集
cannot import name 'import_string' from 'werkzeug' [bug solution]
随机推荐
OAK-FFC-4P全网首次测试
密码字典生成工具pydictor/john
bash shell数组详解
学习在微信小程序中判断url的文件后缀格式
Win11不识别蓝牙适配器的解决方法
leetcode单调栈经典例题——最大矩形
LVS+Keepalived群集部署
高级转录组分析和R数据可视化火热报名中(2022.10)
双向带头循环链表实现
SVG 的 path 属性绘制图形
蜜芽CEO刘楠:垂直电商黄金时代已落幕 坚定转型品牌之路
Inheritance and the static keyword
HCIP 交换实验
HCIP 第十八天
MindSpore:【model_zoo】【resnet】尝试用THOR优化器运行时报cannot import name ‘THOR‘
Techwiz OLED:OLED器件的发光效率
XCTF-easy_Maze
如何直击固定资产管理的难题?
LVS-DR集群部署
JDBC知识点