当前位置:网站首页>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();
边栏推荐
猜你喜欢
参数优化。
冰蝎工具开发实现动态二进制加密WebShell
加降息与BTC流动性事件策略研究
【COS 加码福利】COS 用户实践有奖征文,等你来投稿!
Layer 3 Switch/Router OSPF Configuration Details [Huawei eNSP Experiment]
leetcode动态规划经典例题——53.最大子数组和
usb设备复合g_webcam摄像头码流传输功能以及g_serial串口功能
leetcode经典例题——49.字母异位词分组
[Punctuality Atom STM32 Serial] Chapter 3 Development Environment Construction Excerpted from [Punctual Atom] MiniPro STM32H750 Development Guide_V1.1
leetcode经典例题——56.合并区间
随机推荐
2022 Cloud Native Computing代表厂商 | 灵雀云第三次入选Gartner中国ICT技术成熟度曲线报告
LeetCode 54. 螺旋矩阵 蛇形矩阵式输出字符串
Apache APISIX 2.15 版本发布,为插件增加更多灵活性
无代码平台多项选择入门教程
LeetCode中等题之旋转图像
MindSpore:Batchnorm only support nchw input!
leetcode二叉树系列(二)
rk3399-339 usb设备复合 总体流程
LVS+Keepalived群集部署
MindSpore:Ascend运行出现问题
LVS-DR集群部署
The difference between Mysql application log time and system time is eight hours
matlab练习程序(多线段交点)
VSCode学习资料
Qt:小的任务管理器(task)
OAK-FFC-4P全网首次测试
leetcode每天5题-Day06
Libtomcrypt AES 加密及解密
TCP协议 - 三次握手 - 四次挥手-内核参数调优
LVS负载均衡群集