CompletableFuture異步線程

  • 2020 年 8 月 13 日
  • 筆記

 1、線程池七大參數介紹

(1)corePoolSize:線程池中常駐核心線程數

(2)maximumPoolSize:線程池能夠容納同時執行的最大線程數,此值必須大於等於1

(3)keepAliveTime:多餘的空閑線程存活時間。當前線程池數量超過corePoolSize時,當空閑時間到達keepAliveTime值時,多餘空閑線程會被銷毀直到只剩下corePoolSize個線程為止。

(4)unit:keepAliveTime的時間單位

(5)BlockingQueue:阻塞隊列,如果任務很多,就將在目前多的任務放在隊列里。只要有空閑,就回去隊列里去除最新的任務執行。

(6)threadFactory:表示生成線程池中的工作線程的線程工廠,用於創建線程,一般為默認線程工廠即可

(7)handler:拒絕策略,表示當隊列滿了並且工作線程大於等於線程池的最大線程數(maximumPoolSize)時如何來拒絕來請求的Runnable的策略

 

2、運行順序

1、線程池創建,準備好core數量的核心線程,準備接受任務。

2、新的任務進來,用core準備好的空閑線程執行。

(1)、core滿了,就將再進來的任務放入阻塞隊列中。空閑的core就會自己去阻塞隊

列獲取任務執行

(2)、阻塞隊列滿了,就直接開新線程執行,最大只能開到ma指定的數量

(3)、max都執行好了。max-core數量空閑的線程會在 keepAliveTime指定的時間後自

動銷毀。最終保持到core大小

(4)、如果線程數開到了max的數量,還有新任務進來,就會使用 reject指定的拒絕策

略進行處理

3、所有的線程創建都是由指定的 factory創建的。

 

3、異步對象

 CompletableFuture提供了四個靜態方法來創建一個異步操作。

 1、static CompletableFuture<void> runAsync(Runnable runnable)

 2、public static CompletableFuture <void> runAsync(Runnable runnable, Executor executor)

3、public static <U> CompletableFuture<U> SupplyAsync(Supplier <U> supplier)

4、 public static <U> CompletableFuture<U> supplyAsync(Supplier <U> supplier, Executor executor)

 runXxoox都是沒有返回結果的, supplyXox都是可以獲取返回結果的

 

4、計算完成時回調方法 

public CompletableFuture<T> whenCompleteBiConsumer<? super T,? super Throwable> action);

public CompletableFuture<T> whenCompleteAsyn(BiConsumer<? super T,? super Throwable>action):

public CompletableFuture<T> whenCompleteAsyncBiconsumer<? super T,? super Throwable>action, Executor executor);

public CompletableFuture<T> exceptionallyFunction<Throwable, extends T> fn);

 

 whenComplete可以處理正常和異常的計算結果但是無法修改返回數據, exceptionally處理異常情況並且可以修改返回數據。

 

 whenComplete和 whenCompleteAsync的區別:

  •  whenComplete:是執行當前任務的線程執行繼續執行 whenComplete的任務。
  •  whenCompleteAsync:是執行把 whenCompleteAsync這個任務繼續提交給線程池來進行執行。

方法不以 Async結尾,意味着 Action使用相同的線程執行,而 Async可能會使用其他線程執行(如果是使用相同的線程池,也可能會被同一個線程選中執行)

 

5、 handle方法

 public Completionstage< handleBiFunction<? super T, Throwable, extends U> fn):

 public <U> CompletionstagecU> handleAsyneBiFunction<? super T, Throwable, extends U> fn);

 public <U> CompletionstagecU> handleAsyncBiFunction<? super T, Throwable, extends U> fn, Executor executor);

  • 和 complete一樣,可對結果做最後的處理(可處理異常),可改變返回值。

 

6、兩任務組合-都要完成

 public <U,V> CompletableFuturec <V> thenCombine( Completionstage< ? extends U> other, BiFunction<? super T,? super U,? extends> fn);

 public<U,V> CompletableFuture <V> thenCombineAsync( CompletionStage<? extends> other, BiFunction<? super T.? super U,? extends> fn);

 public <U,V> CompletableFuture<V> thenCombineAsync(CompletionStage<? extends U> other, BiFunction<? super T,? super U,? extends> fn, Executor executor);

 

 public <U> CompletableFuture<Void> thenAcceptBoth(Completionstage<? extends U> other,Biconsumer< super T, super> action);

 public <U> CompletableFuturevoid <Void> thenAcceptBothasync(Completionstage<? extends U> other, Biconsumer<? super T, super U> action);

 public <U> CompletableFuture<Void> thenAcceptBothasync(Completionstage<? extends U> other,Biconsumer<? super T, super Uaction, Executor executor);

 

 public CompletableFuture<Void> runAfterBotl(Completionstage<?> other,Runnable action);

 public CompletableFuture<Void> runAfterBothAsync(CompletionStage<?> other, Runnable action);

 public CompletableFuture<Void> runAfterBothAsync(Completionstage<?> other,Runnable action, Executor executor);

  • 兩個任務必須都完成,觸發該任務。
  •  thenCombine:組合兩個 future,獲取兩個 future的返回結果,並返回當前任務的返回值;
  • thenAcceptBoth:組合兩個 future,獲取兩個 future任務的返回結果,然後處理任務,沒有返回值;
  •  runAfterBoth:組合兩個 future,不需要獲取 future的結果,只需兩個future處理完任務後,處理該任務。

 

7、多任務組合

public static CompletableFuture<> allof(CompletableFuture<?>… cfs);

public static CompletableFuture> anyof(CompletableFuture<>…cfs);

  •  allof:等待所有任務完成
  •  anyOf:只要有一個任務完成