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===");
});
}
}

最新文章

  1. linux 环境下运行STS时 出现must be available in order to run STS
  2. HTTP TCP UDP Socket 关系的几个经典图
  3. hihoCoder#1039
  4. C# Use Pop3Client to read gmail
  5. Javascript基础系列之(六)循环语句(do while循环)
  6. IIS Handler and Module探索
  7. initrd.gz的解压和制作
  8. PL/SQL(二):变量
  9. TopCoder中插件的用法
  10. hunnu-11546--Sum of f(x)
  11. RH033读书笔记(3)-Lab 4 Browsing the Filesystem
  12. c3p0、dbcp、tomcat jdbc pool 连接池配置简介及常用数据库的driverClass和驱动包
  13. 11154 LRC才不会告诉你们的事情
  14. Android硬件编解码与软件编解码
  15. Python基础数据类型之字典
  16. UE4 C++与蓝图交互
  17. java自带dom工具使用实例
  18. Android项目刮刮奖详解扩展篇——开源刮刮奖View的制作
  19. Flink学习(二)Flink中的时间
  20. CF980E The Number Games

热门文章

  1. ABAP ALV显示前排序合并及布局显示
  2. python基础(18):初识模块、re模块
  3. 骚操作!曾经爱过!用 Python 清理收藏夹里已失效的网站
  4. CSS3 过渡---transition
  5. form分辨率
  6. Kotlin介绍(非原创)
  7. iOS 禁用`URL Scheme`和`Universal Link`(通用链接)
  8. Vue实战狗尾草博客后台管理系统第三章
  9. [收藏]Dubbo官方资料
  10. BayaiM__ oracle函数_02