CompletableFuture1
public class CompletableFutureTest {
public static void main(String[] args) throws Exception {
test5();
}
/**
* whenCompleteAsync指的是异步执行传入的BiConsumer
* whenComplete 指的是同步执行传入的BiConsumer
*/
public static void test1() throws ExecutionException, InterruptedException {
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "hello");
//future.whenCompleteAsync((v, r) -> {
future.whenComplete((v, r) -> {
System.out.println("=========");
try {
TimeUnit.SECONDS.sleep();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("====over=====");
});
System.out.println("^^^^^^^^^^");
System.out.println(future.get());
Thread.currentThread().join();
}
/**
* 同样有异步和同步两种方法,thenApply没有异常处理
* @throws ExecutionException
* @throws InterruptedException
*/
public static void test2() throws ExecutionException, InterruptedException {
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> "hello")
.thenApply((s) -> {
try {
System.out.println("==========");
TimeUnit.SECONDS.sleep();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("====over=====");
return s.length();
});
// .thenApplyAsync((s) -> {
// try {
// System.out.println("==========");
// TimeUnit.SECONDS.sleep(5);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// System.out.println("====over=====");
// return s.length();
// });
System.out.println("^^^^^^^^^^");
System.out.println(future.get());
Thread.currentThread().join();
}
/**
* handleAsync 有异常处理
* @throws ExecutionException
* @throws InterruptedException
*/
public static void test3() throws ExecutionException, InterruptedException {
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> "hello")
.handleAsync((v, t) -> {
return v.length();
});
System.out.println(future.get());
}
/**
* thenAcceptAsync 直接将上一个的结果进行消费
* @throws ExecutionException
* @throws InterruptedException
*/
public static void test4() throws ExecutionException, InterruptedException {
CompletableFuture.supplyAsync(() -> "hello")
.thenAcceptAsync((x) -> {
System.out.println(x);
});
}
/**
*执行完上一个future后再执行一个runnable
* @throws ExecutionException
* @throws InterruptedException
*/
public static void test5() throws ExecutionException, InterruptedException {
CompletableFuture.supplyAsync(() -> "hello")
.thenRunAsync(() -> {
System.out.println("====over===");
});
}
}
CompletableFuture1的更多相关文章
- juc多线程编程学习
JUC是java.util.concurrent的缩写,java.util.concurrent是在并发编程中使用的工具类. 在以前的解决并发问题,一般是通过Synchronize关键字,现在可以通过 ...
- JUC 并发编程--04 常用的辅助类CountDownLatch , CyclicBarrier , Semaphore , 读写锁 , 阻塞队列,CompletableFuture(异步回调)
CountDownLatch 相当于一个减法计数器, 构造方法指定一个数字,比如6, 一个线程执行一次,这个数字减1, 当变为0 的时候, await()方法,才开始往下执行,, 看这个例子 Cycl ...
随机推荐
- 深入理解java中的byte类型
作者 | 进击的石头--GO! 来源 | https://www.cnblogs.com/zl181015/p/9435035.html#4432849 Java也提供了一个byte数据类型,并且是基 ...
- 用Java实现二叉查找树
二叉查找树的实现 1. 原理 二叉查找树,又称为二叉排序树.二叉搜索树.对于树中每一个节点X,它的左子树中所有项的值小于X中的项,而它的右子树中所有项的值大于X中的项.二叉查找树的平均深度为O(log ...
- Java开发桌面程序学习(五)——文件选择器和目录选择器的使用
选择器的使用 DirectoryChooser目录选择器官方文档 FileChooser文件选择器官方文档 文件选择器的使用 JavaFx中有个FileChoser,可以打开一个对话框来选择文件 Fi ...
- C# copy files from source directory to destination file and rename repeated files and does not override
static void CopyFiles() { string sourceDir = @"D:\C\ll"; string destDir = @"D:\LL&quo ...
- layui常用的验证
var LayVerifyExtend = { notnullNonnegativeInteger: function (value, item) { //value:表单的值.item:表单的DOM ...
- django支付宝支付集成
概要 本文是关于django集成支付宝[电脑网站支付]功能的过程记录. 功能逻辑主要分为两块: 用户下单跳转至支付宝支付界面 支付成功的回调验证 本文用到的alipay-sdk是基于阿里开发文档htt ...
- SparkStreaming 整合kafka Demo
这里使用的是低级API,因为高级API非常不好用,需要繁琐的配置,也不够自动化,却和低级API的效果一样,所以这里以低级API做演示 你得有zookeeper和kafka 我这里是3台节点主机 架构图 ...
- Windows远程桌面多用户登录的问题
RDP WRAPPER 同时登录 多用户补丁 https://cloud.tencent.com/developer/article/1460728 解决系统更新导致无法多用户登录的问题 问题 ...
- 无依赖单文件http-ftp文件服务器(py2)
网上看到的东东,居然在很合适堡垒机上传下载文件的场景. 这个只能在python2版本上跑, 我更改了自定义端口. 下次再改写个py3版本的. #!/usr/bin/env python "& ...
- leetcode 排序问题
1.堆排序 //大顶堆的构造,传入的i是父节点 void HeapAdjust(int k[],int p,int n) { int i,temp; temp = k[p]; * p; i <= ...