成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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架構師
相關推薦

2024-04-18 08:20:27

Java 8編程工具

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

并發編程異步編程

2025-02-06 16:51:30

2024-12-26 12:59:39

2024-01-11 12:14:31

Async線程池任務

2015-06-16 11:06:42

JavaCompletable

2021-03-18 10:12:54

JavaCompletable字符串

2024-10-14 08:29:14

異步編程任務

2024-08-06 09:43:54

Java 8工具編程

2010-03-05 13:46:12

Android編程學習

2023-04-13 07:33:31

Java 8編程工具

2025-04-07 08:20:00

ORMPython代碼

2025-06-26 02:11:00

2013-04-01 15:38:54

異步編程異步編程模型

2022-03-30 07:32:10

JDK8異步編程

2016-10-21 11:04:07

JavaScript異步編程原理解析

2017-12-21 15:48:11

JavaCompletable
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 亚洲视频区 | 天堂中文资源在线 | 国产精品久久久久久久久久免费看 | 九九热热九九 | 中文字幕av在线 | 久久精品欧美一区二区三区麻豆 | 国产福利一区二区 | 成人免费黄色 | 国产精品美女久久久av超清 | 中文字幕高清av | 久久人人网 | 尤物视频在线免费观看 | 在线观看成人 | 成年人国产在线观看 | 国产福利一区二区 | 成人欧美一区二区三区色青冈 | 午夜影院在线观看 | 精精国产xxxx视频在线播放 | 高清视频一区二区三区 | 亚洲精品www| 色婷婷一区二区三区四区 | a在线观看免费 | 国产精品精品久久久 | 精品中文字幕视频 | 一区二区三区在线播放 | 亚洲日本一区二区三区四区 | 国产一区二区电影 | 久久99精品久久久 | 日本久久久影视 | 国产精品区二区三区日本 | 亚洲精品国产第一综合99久久 | 国产成人精品一区二区 | 欧美在线视频一区 | 久久久一区二区 | 福利精品 | 亚洲精品久久久久久久久久久久久 | 亚洲国产精品久久人人爱 | 自拍偷拍中文字幕 | av一区二区三区四区 | 欧美成ee人免费视频 | 在线观看免费黄色片 |