当前位置:网站首页>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();
边栏推荐
- MindSpore:Batchnorm only support nchw input!
- HTB-Sense
- 2022 Cloud Native Computing代表厂商 | 灵雀云第三次入选Gartner中国ICT技术成熟度曲线报告
- LeetCode581+621+207
- LVS负载均衡群集
- 冰蝎逆向初探
- Win11系统重装用什么好 一键重装Win11教程
- [Punctuality Atom STM32 Serial] Chapter 4 STM32 First Experience Excerpted from [Punctual Atom] MiniPro STM32H750 Development Guide_V1.1
- Qt:小的任务管理器(task)
- Interview at 14:00 in the afternoon, I came out at 14:08 with my head down, asking too much...
猜你喜欢
随机推荐
Shell编程的条件语句
双重for循环案例以及while循环和do while循环案例
Inheritance and the static keyword
《福格行为模型》:如何养成好习惯?
[代码阅读] CycleGAN: Unpaired Image-To-Image Translation Using Cycle-Consistent Adversarial Networks
iMeta | 百度认证完成,搜索“iMeta”直达出版社主页和投稿链接
关于ARM2440中断源个数的一点想法[通俗易懂]
栈与队列的实现
js文字转语音播报
[Punctuality Atom STM32 Serial] Chapter 3 Development Environment Construction Excerpted from [Punctual Atom] MiniPro STM32H750 Development Guide_V1.1
关于DSP驱动外挂flash
常用的输入对象
SVG 的 path 属性绘制图形
使用ClickHouse分析COS的清单和访问日志
Detailed explanation of NAT/NAPT address translation (internal and external network communication) technology [Huawei eNSP]
被Win11安全中心误删除的文件怎么恢复?
什么是元宇宙?
matlab练习程序(多线段交点)
开源一夏 | 查询分页不只有limit,这四种分页方法值得掌握
[Punctuality Atom STM32 Serial] Chapter 2 STM32 Introduction Excerpted from [Punctual Atom] MiniPro STM32H750 Development Guide_V1.1