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 ...
随机推荐
- vue记事2
1.vue2父子组件双向数据传递 https://segmentfault.com/a/1190000011783590 2.vue父组件通过props向子组件传递方法的方式 https://segm ...
- java高并发系列 - 第10天:线程安全和synchronized关键字
这是并发系列第10篇文章. 什么是线程安全? 当多个线程去访问同一个类(对象或方法)的时候,该类都能表现出正常的行为(与自己预想的结果一致),那我们就可以所这个类是线程安全的. 看一段代码: pack ...
- EF-入门操作
EntityFramework Core 理解 DbContext :数据库 DbSet: 数据库表 Model : 数据行 IQueryable<Model> 查询结果集合 Lamada ...
- LinkedHashMap,源码解读就是这么简单
概述 LinkedHashMap是HashMap的子类,它的大部分实现与HashMap相同,两者最大的区别在于,HashMap的对哈希表进行迭代时是无序的,而LinkedHashMap对哈希表迭代是有 ...
- Java日期时间API系列5-----Jdk7及以前的日期时间类TimeUnit在并发编程中的应用
TimeUnit是一个时间单位枚举类,主要用于并发编程,时间单元表示给定粒度单元的时间持续时间,并提供实用程序方法来跨单元转换,以及在这些单元中执行计时和延迟操作. 1.时间单位换算 (1)支持的单位 ...
- 用ggplot包画一个简单饼图
首先用library函数加载ggplot2包 library(ggplot2) library(dplyr) library(tidyr) library(splines) 接下来,进行数据准备: d ...
- centos 安装gitlab
1.开始安装依赖软件:yum -y install policycoreutils openssh-server openssh-clients postfix 2.设置postfix开机自启动,po ...
- JS基础语法---阶段复习+作业练习+接下来知识点heads up
调试:调试代码---高级程序员都是从调试开始的 调试: 写代码---打开浏览器--F12(开发人员工具)--->Sources---双击文件,在某一行代码前面点击一下(出现的东西就是断点) 一元 ...
- jsp表单更新数据库
和插入语句相似,表单传值,在另一个页面接收数据并连接数据库进行更新: 语句如下: <% request.setCharacterEncoding("UTF-8"); Stri ...
- 后端返回null,前端怎么处理?数据容错——不用过分相信外部数据
场景 我们在开发过程当中,总是会遇到因为数据原因,导致使用数组方法或者获取对象属性的时候报错. xxx is not fuction Cannot read property xxxx of unde ...