Java 8 异步编程 CompletableFuture 全解析

开发 后端
Future 是 Java 5 添加的类,用来描述一个异步计算的结果。你可以使用 isDone() 方法检查计算是否完成,或者使用 get() 方法阻塞住调用线程,直到计算完成返回结果,也可以使用 cancel() 方法停止任务的执行。

[[382705]]

本文转载自微信公众号「KK架构师」,作者wangkai 。转载本文请联系KK架构师公众号。   

本文大纲速看

 

一、异步编程

通常来说,程序都是顺序执行,同一时刻只会发生一件事情。如果一个函数依赖于另一个函数的结果,它只能等待那个函数结束才能继续执行,从用户角度来说,整个程序才算执行完毕。但现在的计算机普遍拥有多核 CPU,在那里干等着毫无意义,完全可以在另一个处理器内核上干其他工作,耗时长的任务结束之后会主动通知你。这就是异步编程的出发点:充分使用多核 CPU 的优势,最大程度提高程序性能。一句话来说:所谓异步编程,就是实现一个无需等待被调用函数的返回值而让操作继续运行的方法。

二、抛出一个问题:如何实现烧水泡茶的程序

 

最后我们会使用传统方式和 Java8 异步编程方式分别实现,来对比一下实现复杂度。

三、Java5 的 Future 实现的异步编程

Future 是 Java 5 添加的类,用来描述一个异步计算的结果。你可以使用 isDone() 方法检查计算是否完成,或者使用 get() 方法阻塞住调用线程,直到计算完成返回结果,也可以使用 cancel() 方法停止任务的执行。

  1. public static void main(String[] args) throws InterruptedException, ExecutionException { 
  2.         ExecutorService es = Executors.newFixedThreadPool(5); 
  3.         Future<Integer> f = es.submit(() -> 100); 
  4.         System.out.println(f.get()); 
  5.         es.shutdown(); 
  6.     } 

虽然 Future 提供了异步执行任务的能力,但是对于结果的获取却是很不方便,只能通过阻塞或者轮询的方式得到任务的结果。阻塞的方式显然和我们异步编程的初衷相违背,轮询的方式又会耗费无谓的 CPU 资源,而且也不能及时的获取结果。

当然,很多其他的语言采用回调的方式来实现异步编程,比如 Node.js;Java 的一些框架,比如 Netty,Google Guava 也扩展了 Future 接口,提供了很多回调的机制,封装了工具类,辅助异步编程开发。

Java 作为老牌编程语言,自然也不会落伍。在 Java 8 中,新增了一个包含 50 多个方法的类:CompletableFuture,提供了非常强大的 Future 扩展功能,可以帮助我们简化异步编程的复杂性,提供函数式编程的能力。

四、CompletableFuture 类功能概览

如下图是 CompletableFuture 实现的接口:

 

 

它实现了 Future 接口,拥有 Future 所有的特性,比如可以使用 get() 方法获取返回值等;还实现了 CompletionStage 接口,这个接口有超过 40 个方法,功能太丰富了,它主要是为了编排任务的工作流。

我们可以把工作流和工作流之间的关系分类为三种:串行关系,并行关系,汇聚关系。

串行关系

 

提供了如下的 api 来实现(先大致浏览一遍):

  1. CompletionStage<R> thenApply(fn); 
  2. CompletionStage<R> thenApplyAsync(fn); 
  3. CompletionStage<Void> thenAccept(consumer); 
  4. CompletionStage<Void> thenAcceptAsync(consumer); 
  5. CompletionStage<Void> thenRun(action); 
  6. CompletionStage<Void> thenRunAsync(action); 
  7. CompletionStage<R> thenCompose(fn); 
  8. CompletionStage<R> thenComposeAsync(fn); 

并行关系

 

 

多线程异步执行就是并行关系

汇聚关系

 

汇聚关系,又分为 AND 汇聚关系和 OR 汇聚关系:

AND 汇聚关系,就是所有依赖的任务都完成之后再执行;OR 汇聚关系,就是依赖的任务中有一个执行完成,就开始执行。

AND 汇聚关系由这些接口表达:

  1. CompletionStage<R> thenCombine(other, fn); 
  2. CompletionStage<R> thenCombineAsync(other, fn); 
  3. CompletionStage<Void> thenAcceptBoth(other, consumer); 
  4. CompletionStage<Void> thenAcceptBothAsync(other, consumer); 
  5. CompletionStage<Void> runAfterBoth(other, action); 
  6. CompletionStage<Void> runAfterBothAsync(other, action); 

OR 汇聚关系由这些接口来表达:

  1. CompletionStage applyToEither(other, fn); 
  2. CompletionStage applyToEitherAsync(other, fn); 
  3. CompletionStage acceptEither(other, consumer); 
  4. CompletionStage acceptEitherAsync(other, consumer); 
  5. CompletionStage runAfterEither(other, action); 
  6. CompletionStage runAfterEitherAsync(other, action); 

五、CompletableFuture 接口精讲

1、提交执行的静态方法

方法名描述

方法名 描述
runAsync(Runnable runnable) 执行异步代码,使用 ForkJoinPool.commonPool() 作为它的线程池
runAsync(Runnable runnable, Executor executor) 执行异步代码,使用指定的线程池
supplyAsync(Supplier<U> supplier) 异步执行代码,有返回值,使用 ForkJoinPool.commonPool() 作为它的线程池
supplyAsync(Supplier<U> supplier, Executor executor) 异步执行代码,有返回值,使用指定的线程池执行

上述四个方法,都是提交任务的,runAsync 方法需要传入一个实现了 Runnable 接口的方法,supplyAsync 需要传入一个实现了 Supplier 接口的方法,实现 get 方法,返回一个值。

(1)run 和 supply 的区别

run 就是执行一个方法,没有返回值,supply 执行一个方法,有返回值。

(2)一个参数和两个参数的区别

第二个参数是线程池,如果没有传,则使用自带的 ForkJoinPool.commonPool() 作为线程池,这个线程池默认创建的线程数是 CPU 的核数(也可以通过 JVM option:-Djava.util.concurrent.ForkJoinPool.common.parallelism 来设置 ForkJoinPool 线程池的线程数)

2、串行关系 api

这些 api 之间主要是能否获得前一个任务的返回值与自己是否有返回值的区别。

api 是否可获得前一个任务的返回值 是否有返回值
thenApply
thenAccept
thenRun 不能
thenCompose

(1) thenApply 和 thenApplyAsync 使用

thenApply 和 thenApplyAsync 把两个并行的任务串行化,另一个任务在获得上一个任务的返回值之后,做一些加工和转换。它也是有返回值的。

  1. public class BasicFuture4 { 
  2.  
  3.     @Data 
  4.     @AllArgsConstructor 
  5.     @ToString 
  6.     static class Student { 
  7.         private String name
  8.     } 
  9.      
  10.     public static void main(String[] args) throws ExecutionException, InterruptedException { 
  11.         CompletableFuture<Student> future = CompletableFuture.supplyAsync(() -> "Jack"
  12.                 .thenApply(s -> s + " Smith"
  13.                 .thenApply(String::toUpperCase) 
  14.                 .thenApplyAsync(Student::new); 
  15.         System.out.println(future.get()); 
  16.     } 
  17.  

结果可以看到,输入是一个字符串,拼接了一个字符串,转换成大写,new 了一个 Student 对象返回。

  1. BasicFuture4.Student(name=JACK SMITH) 

和 thenApply 一起的还有 thenAccept 和 thenRun,thenAccept 能获得到前一个任务的返回值,但是自身没有返回值;thenRun 不能获得前一个任务的返回值,自身也没有返回值。

(2)thenApply 和 thenApplyAsync 的区别

这两个方法的区别,在于谁去执行任务。如果使用 thenApplyAsync,那么执行的线程是从 ForkJoinPool.commonPool() 或者自己定义的线程池中取线程去执行。如果使用 thenApply,又分两种情况,如果 supplyAsync 方法执行速度特别快,那么 thenApply 任务就使用主线程执行,如果 supplyAsync 执行速度特别慢,就是和 supplyAsync 执行线程一样。

可以使用下面的例子演示一下:

  1. package com.dsj361.future; 
  2.  
  3. import java.util.concurrent.CompletableFuture; 
  4. import java.util.concurrent.ExecutionException; 
  5.  
  6. /** 
  7.  * @Author wangkai 
  8.  */ 
  9. public class BasicFuture8 { 
  10.  
  11.     public static void main(String[] args) throws ExecutionException, InterruptedException { 
  12.         System.out.println("----------supplyAsync 执行很快"); 
  13.         CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> { 
  14.             System.out.println(Thread.currentThread().getName()); 
  15.             return "1"
  16.         }).thenApply(s -> { 
  17.             System.out.println(Thread.currentThread().getName()); 
  18.             return "2"
  19.         }); 
  20.         System.out.println(future1.get()); 
  21.  
  22.         System.out.println("----------supplyAsync 执行很慢"); 
  23.         CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> { 
  24.             try { 
  25.                 Thread.sleep(1000); 
  26.             } catch (InterruptedException e) { 
  27.             } 
  28.             System.out.println(Thread.currentThread().getName()); 
  29.             return "1"
  30.         }).thenApply(s -> { 
  31.             System.out.println(Thread.currentThread().getName()); 
  32.             return "2"
  33.         }); 
  34.         System.out.println(future2.get()); 
  35.     } 

执行结果:

  1. ----------supplyAsync 执行很快 
  2. ForkJoinPool.commonPool-worker-1 
  3. main 
  4. ----------supplyAsync 执行很慢 
  5. ForkJoinPool.commonPool-worker-1 
  6. ForkJoinPool.commonPool-worker-1 

(3)thenCompose 的使用

假设有两个异步任务,第二个任务想要获取第一个任务的返回值,并且做运算,我们可以用 thenCompose。此时使用 thenApply 也可以实现,看一段代码发现他们的区别:

  1. public class BasicFuture9 { 
  2.  
  3.     public static void main(String[] args) throws ExecutionException, InterruptedException { 
  4.         CompletableFuture<String> future = getLastOne().thenCompose(BasicFuture9::getLastTwo); 
  5.         System.out.println(future.get()); 
  6.  
  7.         CompletableFuture<CompletableFuture<String>> future2 = getLastOne().thenApply(s -> getLastTwo(s)); 
  8.         System.out.println(future2.get().get()); 
  9.     } 
  10.  
  11.     public static CompletableFuture<String> getLastOne(){ 
  12.         return CompletableFuture.supplyAsync(()-> "topOne"); 
  13.     } 
  14.  
  15.     public static CompletableFuture<String> getLastTwo(String s){ 
  16.         return CompletableFuture.supplyAsync(()-> s + "  topTwo"); 
  17.     } 

可以看到使用 thenApply 的时候,需要使用两个 get() 方法才能获取到最终的返回值,使用 thenCompose 只要一个即可。

3、And 汇聚关系 Api

(1)thenCombine 的使用

加入我们要计算两个异步方法返回值的和,就必须要等到两个异步任务都计算完才能求和,此时可以用 thenCombine 来完成。

  1. public static void main(String[] args) throws ExecutionException, InterruptedException { 
  2.     CompletableFuture<Integer> thenComposeOne = CompletableFuture.supplyAsync(() -> 192); 
  3.     CompletableFuture<Integer> thenComposeTwo = CompletableFuture.supplyAsync(() -> 196); 
  4.     CompletableFuture<Integer> thenComposeCount = thenComposeOne 
  5.         .thenCombine(thenComposeTwo, (s, y) -> s + y); 
  6.  
  7.     thenComposeOne.thenAcceptBoth(thenComposeTwo,(s,y)-> System.out.println("thenAcceptBoth")); 
  8.     thenComposeOne.runAfterBoth(thenComposeTwo, () -> System.out.println("runAfterBoth")); 
  9.  
  10.     System.out.println(thenComposeCount.get()); 

可以看到 thenCombine 第二个参数是一个 Function 函数,前面两个异步任务都完成之后,使用这个函数来完成一些运算。

(2)thenAcceptBoth

接收前面两个异步任务的结果,执行一个回调函数,但是这个回调函数没有返回值。

(3)runAfterBoth

接收前面两个异步任务的结果,但是回调函数,不接收参数,也不返回值。

4、Or 汇聚关系 Api

  1. public class BasicFuture11 { 
  2.  
  3.     public static void main(String[] args) throws ExecutionException, InterruptedException { 
  4.         CompletableFuture<Integer> thenComposeOne = CompletableFuture.supplyAsync(() -> 192); 
  5.         CompletableFuture<Integer> thenComposeTwo = CompletableFuture.supplyAsync(() -> 196); 
  6.         CompletableFuture<Integer> thenComposeCount = thenComposeOne 
  7.                 .applyToEither(thenComposeTwo, s -> s + 1); 
  8.  
  9.         thenComposeOne.acceptEither(thenComposeTwo,s -> {}); 
  10.          
  11.         thenComposeOne.runAfterEither(thenComposeTwo,()->{}); 
  12.  
  13.         System.out.println(thenComposeCount.get()); 
  14.     } 

(1)applyToEither

任何一个执行完就执行回调方法,回调方法接收一个参数,有返回值

(2)acceptEither

任何一个执行完就执行回调方法,回调方法接收一个参数,无返回值

(3)runAfterEither

任何一个执行完就执行回调方法,回调方法不接收参数,也无返回值

5、处理异常

上面我们讲了如何把几个异步任务编排起来,执行一些串行或者汇聚操作。还有一个重要的地方,就是异常的处理。

先看下面的例子:

  1. public static void main(String[] args) throws ExecutionException, InterruptedException { 
  2.     CompletableFuture.supplyAsync(() -> { 
  3.         System.out.println("execute one "); 
  4.         return 100; 
  5.     }) 
  6.         .thenApply(s -> 10 / 0) 
  7.         .thenRun(() -> System.out.println("thenRun")) 
  8.         .thenAccept(s -> System.out.println("thenAccept")); 
  9.  
  10.     CompletableFuture.runAsync(() -> System.out.println("other")); 

结果:

  1. execute one  
  2. other 

可以发现,只要链条上有一个任务发生了异常,这个链条下面的任务都不再执行了。

但是 main 方法上的接下来的代码还是会执行的。

所以这个时候,需要合理的去处理异常来完成一些收尾的工作。

  1. public class BasicFuture12 { 
  2.  
  3.     public static void main(String[] args) throws ExecutionException, InterruptedException { 
  4.         CompletableFuture.supplyAsync(() -> { 
  5.             System.out.println("execute one "); 
  6.             return 100; 
  7.         }) 
  8.                 .thenApply(s -> 10 / 0) 
  9.                 .thenRun(() -> System.out.println("thenRun")) 
  10.                 .thenAccept(s -> System.out.println("thenAccept")) 
  11.                 .exceptionally(s -> { 
  12.                     System.out.println("异常处理"); 
  13.                     return null
  14.                 }); 
  15.  
  16.         CompletableFuture.runAsync(() -> System.out.println("other")); 
  17.     } 

可以使用 exceptionally 来处理异常。

使用 handle() 方法也可以处理异常。但是 handle() 方法的不同之处在于,即使没有发生异常,也会执行。

六、烧水泡茶程序的实现

1、使用 Thread 多线程和 CountDownLatch 来实现

  1. public class MakeTee { 
  2.  
  3.     private static CountDownLatch countDownLatch = new CountDownLatch(2); 
  4.  
  5.     static class HeatUpWater implements Runnable { 
  6.  
  7.         private CountDownLatch countDownLatch; 
  8.  
  9.         public HeatUpWater(CountDownLatch countDownLatch) { 
  10.             this.countDownLatch = countDownLatch; 
  11.         } 
  12.         @Override 
  13.         public void run() { 
  14.             try { 
  15.                 System.out.println("洗水壶"); 
  16.                 Thread.sleep(1000); 
  17.                 System.out.println("烧开水"); 
  18.                 Thread.sleep(5000); 
  19.                 countDownLatch.countDown(); 
  20.             } catch (InterruptedException e) { 
  21.             } 
  22.  
  23.         } 
  24.     } 
  25.  
  26.     static class PrepareTee implements Runnable { 
  27.         private CountDownLatch countDownLatch; 
  28.  
  29.         public PrepareTee(CountDownLatch countDownLatch) { 
  30.             this.countDownLatch = countDownLatch; 
  31.         } 
  32.  
  33.         @Override 
  34.         public void run() { 
  35.             try { 
  36.                 System.out.println("洗茶壶"); 
  37.                 Thread.sleep(1000); 
  38.                 System.out.println("洗茶杯"); 
  39.                 Thread.sleep(1000); 
  40.                 System.out.println("拿茶叶"); 
  41.                 Thread.sleep(1000); 
  42.                 countDownLatch.countDown(); 
  43.             } catch (InterruptedException e) { 
  44.             } 
  45.         } 
  46.     } 
  47.     public static void main(String[] args) throws InterruptedException { 
  48.         new Thread(new HeatUpWater(countDownLatch) ).start(); 
  49.         new Thread(new PrepareTee(countDownLatch)).start(); 
  50.         countDownLatch.await(); 
  51.         System.out.println("准备就绪,开始泡茶"); 
  52.     } 

这里我们使用两个线程,分别执行烧水和泡茶的程序,使用 CountDownLatch 来协调两个线程的进度,等到他们都执行完成之后,再执行泡茶的动作。

可以看到这种方法,多了很多不必要的代码,new Thread,人工维护 CountDownLatch 的进度。

2、使用 CompletableFuture 来实现

  1. public class MakeTeeFuture { 
  2.  
  3.     public static void main(String[] args) throws ExecutionException, InterruptedException { 
  4.         CompletableFuture<Void> future1 = CompletableFuture.runAsync(() -> { 
  5.             try { 
  6.                 System.out.println("洗水壶"); 
  7.                 Thread.sleep(1000); 
  8.                 System.out.println("烧开水"); 
  9.                 Thread.sleep(5000); 
  10.             } catch (InterruptedException e) { 
  11.                 e.printStackTrace(); 
  12.             } 
  13.         }); 
  14.         CompletableFuture<Void> future2 = CompletableFuture.runAsync(() -> { 
  15.             try { 
  16.                 System.out.println("洗茶壶"); 
  17.                 Thread.sleep(1000); 
  18.                 System.out.println("洗茶杯"); 
  19.                 Thread.sleep(1000); 
  20.                 System.out.println("拿茶叶"); 
  21.                 Thread.sleep(1000); 
  22.             } catch (InterruptedException e) { 
  23.                 e.printStackTrace(); 
  24.             } 
  25.         }); 
  26.         CompletableFuture<Void> finish = future1.runAfterBoth(future2, () -> { 
  27.             System.out.println("准备完毕,开始泡茶"); 
  28.         }); 
  29.         System.out.println(finish.get()); 
  30.     } 

这个程序极度简单,无需手工维护线程,给任务分配线程的工作也不需要关注。

同时语义也更加清晰,future1.runAfterBoth(future2,......) 能够清晰的表述“任务 3 要等到任务 1 和任务 2 都完成之后才能继续开始”

然后代码更加简练并且专注于业务逻辑,几乎所有的代码都是业务逻辑相关的。

七、总结

本文介绍了异步编程的概念,以及 Java8 的 CompletableFuture 是如何优雅的处理多个异步任务之间的协调工作的。CompletableFuture 能够极大简化我们对于异步任务编排的工作,Flink 在提交任务时,也是使用这种异步任务的方式,去编排提交时和提交后对于任务状态处理的一些工作的。

责任编辑:武晓燕 来源: KK架构师
相关推荐

2020-05-29 07:20:00

Java8异步编程源码解读

2021-06-06 16:56:49

异步编程Completable

2023-07-19 08:03:05

Future异步JDK

2022-07-08 14:14:04

并发编程异步编程

2015-06-16 11:06:42

JavaCompletable

2024-01-11 12:14:31

Async线程池任务

2021-03-18 10:12:54

JavaCompletable字符串

2023-04-13 07:33:31

Java 8编程工具

2022-03-30 07:32:10

JDK8异步编程

2013-04-01 15:38:54

异步编程异步编程模型

2017-12-21 15:48:11

JavaCompletable

2010-03-05 13:46:12

Android编程学习

2024-03-06 08:13:33

FutureJDKCallable

2016-10-21 11:04:07

JavaScript异步编程原理解析

2021-08-02 11:13:28

人工智能机器学习技术

2021-03-22 08:45:30

异步编程Java

2013-04-01 15:25:41

异步编程异步EMP

2022-05-13 12:34:16

美团开发实践

2020-10-15 13:29:57

javascript

2020-08-18 10:20:50

Java 编程开发
点赞
收藏

51CTO技术栈公众号