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的更多相关文章

  1. juc多线程编程学习

    JUC是java.util.concurrent的缩写,java.util.concurrent是在并发编程中使用的工具类. 在以前的解决并发问题,一般是通过Synchronize关键字,现在可以通过 ...

  2. JUC 并发编程--04 常用的辅助类CountDownLatch , CyclicBarrier , Semaphore , 读写锁 , 阻塞队列,CompletableFuture(异步回调)

    CountDownLatch 相当于一个减法计数器, 构造方法指定一个数字,比如6, 一个线程执行一次,这个数字减1, 当变为0 的时候, await()方法,才开始往下执行,, 看这个例子 Cycl ...

随机推荐

  1. GALAXY OJ NOIP2019联合测试2-普及组

    概要: 今天比了个赛,还挺水,只不过不太理想. 题目: Problem : 韬韬抢苹果 又到了收获的季节,树上结了许多韬韬,错了,是许多苹果,有很多个小韬韬都来摘苹果.每个韬韬都想要最大的苹果,所以发 ...

  2. VUE基础实用技巧

    Vue以前听说过,有了解过一点.当时还在热衷于原生JavaScript去写一些方法的封装,不是为啥,就感觉这样很帅,后面多多少少接触了一些JQuery的用法,到现在为止,JavaScript原生封装的 ...

  3. windows linux 子系统及windows terminal的使用。

    windows linux 子系统及windows terminal的使用. windows linux (wsl) 其实windows早就为我们准备好了子系统,但是我们的应用商店经常挂掉.因此都用不 ...

  4. FCC---Create Visual Direction by Fading an Element from Left to Right---一个带好看背景色的圆形图案,从左到右移动,透明度opacity渐变为0.1,背景色渐渐消失的效果

    For this challenge, you'll change the opacity of an animated element so it gradually fades as it rea ...

  5. arcgis api for javascript 学习(六) 地图打印

    1.本文应用arcgis api for javascript对发布的动态地图进行打印,打印的为PDF格式,打印出来如图: 2.需要特别注意的是:我们在运行代码前,需要打开PrintingTools, ...

  6. 一文解读5G (转)

    今天要研究的对象,是5G接入网. 什么是接入网?相信不少同学,对这个概念一定不会陌生. 搬出这张移动通信架构图: 接入网,在我们无线通信里,一般指无线接入网,也就是通常所说的RAN(Radio Acc ...

  7. C# 运行时的关系

    简介 记录c#对象在托管堆中运行时的相互关系,如下记录了一个方法在执行时候的生命周期,当方法在之前,CLR会先执行将方法里面所有用到的局部变量.参数对应的内存地址等全部存放当前线程栈当中,并且会将所有 ...

  8. 联邦学习(Federated Learning)

    联邦学习简介        联邦学习(Federated Learning)是一种新兴的人工智能基础技术,在 2016 年由谷歌最先提出,原本用于解决安卓手机终端用户在本地更新模型的问题,其设计目标是 ...

  9. layui增删改查

    dao方法 package com.dao; import java.sql.SQLException; import java.util.List; import java.util.Map; im ...

  10. Pwnable-fd

    打开Ubuntu输入ssh fd@pwnable.kr -p2222,连接之后输入密码guest 之后就是ls -l看看里面的文件和权限,fd.fd.c.flag 看看fd.c的源码 #include ...