CompletableFuture的入门

打印 上一主题 下一主题

主题 878|帖子 878|积分 2634

runAsync 和 supplyAsync

runAsync接受一个Runable的实现,无返回值
  1. CompletableFuture.runAsync(()->System.out.println("无返回结果的运行"));
复制代码
supplyAsync接受一个Supplier的实现,有返回值
  1. CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
  2.       System.out.println("有返回结果的运行");
  3.       return 1;
  4.   });
复制代码
获取结果的get和join

都是堵塞,直到返回结果
get方法抛出是经过处理的异常,ExecutionException或**InterruptedException **,需要用户手动捕获
  1. try {
  2.    System.out.println(CompletableFuture.supplyAsync(() -> {
  3.     System.out.println("有返回结果的运行");
  4.     return 1;
  5.   }).get());
  6. } catch (InterruptedException e) {
  7.   e.printStackTrace();
  8. } catch (ExecutionException e) {
  9.   e.printStackTrace();
  10. }
复制代码
join方法抛出的就不用捕获,是经过包装的**CompletionException **或 CancellationException
  1.         System.out.println(CompletableFuture.supplyAsync(() -> {
  2.             try {
  3.                 TimeUnit.MILLISECONDS.sleep(1000);
  4.             } catch (InterruptedException e) {
  5.                 e.printStackTrace();
  6.             }
  7.             System.out.println("有返回结果的运行");
  8.             return 1;
  9.         }).join());
复制代码
常用方法

获取结果的get\join\getNow

get():一直等待
get(timeout,unit):等待,除非超时
getNow(valueIfAbsent):计算完返回计算的结果,未计算完返回默认的结果
  1. CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
  2.             try {
  3.                 TimeUnit.SECONDS.sleep(1);
  4.                 ;
  5.             } catch (InterruptedException e) {
  6.                 e.printStackTrace();
  7.             }
  8.             return 1;
  9.         });
  10.         System.out.println("立即获取:"+completableFuture.getNow(9999));
  11.         try {
  12.             TimeUnit.SECONDS.sleep(2);
  13.             System.out.println("doing");
  14.         } catch (InterruptedException e) {
  15.             e.printStackTrace();
  16.         }
  17.         System.out.println("等一会获取:"+completableFuture.getNow(9999));
复制代码
join() 同get()
thenApply\handle

执行完前面的,前面返回的结果返回,然后传给后面再,执行完后面任务,一步一步来。
  1. CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
  2.     System.out.println("step 1");
  3.     return 1;
  4. }).thenApply(a -> {
  5.     System.out.println("step 2");
  6.     return a + 2;
  7. }).thenApply(a -> {
  8.     System.out.println("step 3");
  9.     return a + 3;
  10. });
  11. System.out.println(completableFuture.get());
复制代码
执行结果:
  1. CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
  2.     System.out.println("step 1");
  3.     int a=1/0;
  4.     return 1;
  5. }).handle((a,b) -> {
  6.     System.out.println("step 2");
  7.     if (b!=null) {
  8.         System.out.println(b.getMessage());
  9.         return 0;
  10.     }
  11.     return a + 2;
  12. }).handle((a,b) -> {
  13.     System.out.println("step 3");
  14.     if (b!=null) {
  15.         System.out.println(b.getMessage());
  16.         return 0;
  17.     }
  18.     return a + 3;
  19. });
  20. System.out.println(completableFuture.get());
复制代码
执行结果:

thenApply和handle的区别:
thenApply执行的时候,有异常的则整个执行链会中断,直接抛出异常。

handle有异常也可以往下一步走,根据带的异常参数可以进一步处理

thenAccept

接收前面任务的返回结果,当前节点处理,并不返回结果。
  1. CompletableFuture.supplyAsync(()->{
  2.     System.out.println("step 1");
  3.     return 10;
  4. }).thenAccept(a->{
  5.     System.out.println("res "+a);
  6. });
复制代码
applyToEither

在多个任务段同时执行时,哪个任务段用时最少,就返回哪个
  1. CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
  2.     System.out.println("step 1");
  3.     try {
  4.         TimeUnit.SECONDS.sleep(1);
  5.     } catch (InterruptedException e) {
  6.         e.printStackTrace();
  7.     }
  8.     return 1;
  9. }).applyToEither(CompletableFuture.supplyAsync(() -> {
  10.     System.out.println("step 2");
  11.     try {
  12.         TimeUnit.SECONDS.sleep(2);
  13.     } catch (InterruptedException e) {
  14.         e.printStackTrace();
  15.     }
  16.     return 2;
  17. }), a -> {
  18.     return a;
  19. });
  20. System.out.println(completableFuture.get());
复制代码
执行结果:

thenCombine

合并多个任务段的返回结果
  1. CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
  2.             System.out.println("step 1");
  3.             return IntStream.range(1, 11).sum();
  4.         }).thenCombine(CompletableFuture.supplyAsync(() -> {
  5.             System.out.println("step 2");
  6.             return IntStream.range(11, 21).sum();
  7.         }), (a, b) -> a + b)
  8.         .thenCombine(CompletableFuture.supplyAsync(() -> {
  9.             System.out.println("step 3");
  10.             return IntStream.range(21, 31).sum();
  11.         }), (a, b) -> a + b);
  12. System.out.println(completableFuture.get());
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

美丽的神话

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表