午夜无码人妻aⅴ大片色欲张津瑜,国产69久久久欧美黑人A片,色妺妺视频网,久久久久国产综合AV天堂

怎么使用CompletableFuture

這篇文章主要講解了“怎么使用CompletableFuture”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“怎么使用CompletableFuture”吧!

10年積累的成都做網(wǎng)站、成都網(wǎng)站制作、成都外貿(mào)網(wǎng)站建設(shè)經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站設(shè)計(jì)后付款的網(wǎng)站建設(shè)流程,更有安寧免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。

怎么使用CompletableFuture

但Future機(jī)制,還不那么靈活,比如怎么去利用Future機(jī)制描述兩個(gè)任務(wù)串行執(zhí)行,又或是兩個(gè)任務(wù)并行執(zhí)行,又或是只關(guān)心最先執(zhí)行結(jié)束的任務(wù)結(jié)果。

Future機(jī)制在一定程度上都無法快速地滿足以上需求,CompletableFuture便應(yīng)運(yùn)而生了。

1. 創(chuàng)建一個(gè)異步任務(wù)

public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier)   public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier,Executor executor);   public static CompletableFuture<Void> runAsync(Runnable runnable);   public static CompletableFuture<Void> runAsync(Runnable runnable,Executor executor);

supplyAsync與runAsync的區(qū)別在于:supplyAsync有返回值,而runAsync沒有返回值

帶Executor參數(shù)的構(gòu)造函數(shù),則使用線程池中的線程執(zhí)行異步任務(wù)(線程池可以參考說說線程池)

不帶Executor參數(shù)的構(gòu)造函數(shù),則使用ForkJoinPool.commonPool()中的線程執(zhí)行異步任務(wù)(Fork/Join框架可以參考談?wù)劜⑿辛鱬arallelStream)

1.1 示例:使用supplyAsync創(chuàng)建一個(gè)有返回值的異步任務(wù)

public class Case1 {      public static void main(String[] args) throws Exception {          CompletableFuture<Integer> completableFuture=CompletableFuture.supplyAsync(()->{             try {                 Thread.sleep(1000);             } catch (InterruptedException e) {                 e.printStackTrace();             }             return 1;         });         //該方法會(huì)一直阻塞         Integer result = completableFuture.get();         System.out.println(result);     }  }

2. 異步任務(wù)的回調(diào)

public CompletableFuture<T> whenComplete(BiConsumer<? super T, ? super Throwable> action);    public CompletableFuture<T> whenCompleteAsync(BiConsumer<? super T, ? super Throwable> action);    public CompletableFuture<T> whenCompleteAsync(BiConsumer<? super T, ? super Throwable> action, Executor executor);    public CompletableFuture<T> exceptionally(Function<Throwable, ? extends T> fn);

whenComplete開頭的方法在計(jì)算任務(wù)完成(包括正常完成與出現(xiàn)異常)之后會(huì)回調(diào)

而exceptionally則只會(huì)在計(jì)算任務(wù)出現(xiàn)異常時(shí)才會(huì)被回調(diào)

如何確定哪個(gè)線程去回調(diào)whenComplete,比較復(fù)雜,先略過。

而回調(diào)whenCompleteAsync的線程比較簡(jiǎn)單,隨便拿一個(gè)空閑的線程即可,后綴是Async的方法同理。

2.1 示例:計(jì)算出現(xiàn)異常,使用whenComplete與exceptionally進(jìn)行處理

package com.qcy.testCompleteableFuture;  import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; import java.util.function.BiConsumer; import java.util.function.Function; import java.util.stream.IntStream;  /**  * @author qcy  * @create 2020/09/07 17:40:44  */ public class Case2 {      public static void main(String[] args) throws Exception {          CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {             try {                 Thread.sleep(1000);             } catch (InterruptedException e) {                 e.printStackTrace();             }             System.out.println("執(zhí)行supplyAsync的線程:" + Thread.currentThread().getName());             int i = 1 / 0;             return 1;         });          completableFuture.whenComplete(new BiConsumer<Integer, Throwable>() {             @Override             public void accept(Integer integer, Throwable throwable) {                 System.out.println("執(zhí)行whenComplete的線程:" + Thread.currentThread().getName());                 if (throwable == null) {                     System.out.println("計(jì)算未出現(xiàn)異常,結(jié)果:" + integer);                 }             }         });          completableFuture.exceptionally(new Function<Throwable, Integer>() {             @Override             public Integer apply(Throwable throwable) {                 //出現(xiàn)異常時(shí),則返回一個(gè)默認(rèn)值                 System.out.println("計(jì)算出現(xiàn)異常,信息:" + throwable.getMessage());                 return -1;             }         });          System.out.println(completableFuture.get());     }  }

輸出:

怎么使用CompletableFuture

當(dāng)然,CompletableFuture內(nèi)的各種方法是支持鏈?zhǔn)秸{(diào)用與Lambda表達(dá)式的,我們進(jìn)行如下改寫:

public static void main(String[] args) throws Exception {       CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {          try {              Thread.sleep(2000);          } catch (InterruptedException e) {              e.printStackTrace();          }          System.out.println("執(zhí)行supplyAsync的線程:" + Thread.currentThread().getName());          int i = 1 / 0;          return 1;      }).whenComplete((integer, throwable) -> {          System.out.println("執(zhí)行whenComplete的線程:" + Thread.currentThread().getName());          if (throwable == null) {              System.out.println("計(jì)算未出現(xiàn)異常,結(jié)果:" + integer);          }      }).exceptionally(throwable -> {          //出現(xiàn)異常時(shí),則返回一個(gè)默認(rèn)值          System.out.println("計(jì)算出現(xiàn)異常,信息:" + throwable.getMessage());          return -1;      });       System.out.println("計(jì)算結(jié)果:" + completableFuture.get());  }

3. 任務(wù)串行化執(zhí)行

public <U> CompletableFuture<U> thenApply(Function<? super T,? extends U> fn);    public CompletableFuture<Void> thenRun(Runnable action);    public CompletableFuture<Void> thenAccept(Consumer<? super T> action);    public <U> CompletableFuture<U> handle(BiFunction<? super T, Throwable, ? extends U> fn);    public <U> CompletableFuture<U> thenCompose(Function<? super T, ? extends CompletionStage<U>> fn);

thenApply,依賴上一次任務(wù)執(zhí)行的結(jié)果,參數(shù)中的Function<? super T,? extends U>,T代表上一次任務(wù)返回值的類型,U代表當(dāng)前任務(wù)返回值的類型,當(dāng)上一個(gè)任務(wù)沒有出現(xiàn)異常時(shí),thenApply才會(huì)被調(diào)用

thenRun,不需要知道上一個(gè)任務(wù)的返回結(jié)果,只是在上一個(gè)任務(wù)執(zhí)行完成之后開始執(zhí)行Runnable

thenAccept,依賴上一次任務(wù)的執(zhí)行結(jié)果,因?yàn)槿雲(yún)⑹荂onsumer,所以不返回任何值。

handle和thenApply相似,不過當(dāng)上一個(gè)任務(wù)出現(xiàn)異常時(shí),能夠執(zhí)行handle,卻不會(huì)去執(zhí)行thenApply

thenCompose,傳入一次任務(wù)執(zhí)行的結(jié)果,返回一個(gè)新的CompleteableFuture對(duì)象

3.1 示例:使用串行化任務(wù)分解兩數(shù)相乘并輸出

package com.qcy.testCompleteableFuture;  import java.util.concurrent.CompletableFuture;  /**  * @author qcy  * @create 2020/09/07 17:40:44  */ public class Case4 {      public static void main(String[] args) {                  CompletableFuture.supplyAsync(() -> 2)                 .thenApply(num -> num * 3)                 .thenAccept(System.out::print);     }  }

很顯然,輸出為6

3.2 示例:使用串行化任務(wù)并且模擬出現(xiàn)異常

package com.qcy.testCompleteableFuture;  import java.util.concurrent.CompletableFuture; import java.util.function.BiFunction;  /**  * @author qcy  * @create 2020/09/07 17:40:44  */ public class Case4 {      public static void main(String[] args) {          CompletableFuture.supplyAsync(() -> 2)                 .thenApply(num -> num / 0)                 .thenApply(result -> result * 3)                 .handle((integer, throwable) -> {                     if (throwable == null) {                         return integer;                     } else {                         throwable.printStackTrace();                         return -1;                     }                 }).thenAccept(System.out::print);     }  }

最終會(huì)輸出-1

4. 任務(wù)同時(shí)執(zhí)行,且都需要執(zhí)行完成

public <U,V> CompletableFuture<V> thenCombine(CompletionStage<? extends U> other, Function<? super T,? super U,? extends V> fn);    public <U> CompletableFuture<Void> thenAcceptBoth(CompletionStage<? extends U> other, Consumer<? super T, ? super U> action);    public CompletableFuture<Void> runAfterBoth(CompletionStage<?> other,Runnable action);    public static CompletableFuture<Void> allOf(CompletableFuture<?>... cfs);

thenCombine,合并兩個(gè)任務(wù),兩個(gè)任務(wù)可以同時(shí)執(zhí)行,都執(zhí)行成功后,執(zhí)行最后的BiFunction操作。其中T代表第一個(gè)任務(wù)的執(zhí)行結(jié)果類型,U代表第二個(gè)任務(wù)的執(zhí)行結(jié)果類型,V代表合并的結(jié)果類型

thenAcceptBoth,和thenCombine特性用法都極其相似,唯一的區(qū)別在于thenAcceptBoth進(jìn)行一個(gè)消費(fèi),沒有返回值

runAfterBoth,兩個(gè)任務(wù)都執(zhí)行完成后,但不關(guān)心他們的返回結(jié)構(gòu),然后去執(zhí)行一個(gè)Runnable。

allOf,當(dāng)所有的任務(wù)都執(zhí)行完成后,返回一個(gè)CompletableFuture

4.1 示例:使用thenCombine合并任務(wù)

package com.qcy.testCompleteableFuture;  import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException;  /**  * @author qcy  * @create 2020/09/07 17:40:44  */ public class Case5 {      public static void main(String[] args) throws Exception {          CompletableFuture<Integer> cf1 = CompletableFuture.supplyAsync(() -> {             System.out.println("任務(wù)1開始");             try {                 Thread.sleep(3000);             } catch (InterruptedException e) {                 e.printStackTrace();             }             System.out.println("任務(wù)1結(jié)束");             return 2;         });          CompletableFuture<Integer> cf2 = CompletableFuture.supplyAsync(() -> {             System.out.println("任務(wù)2開始");             try {                 Thread.sleep(3000);             } catch (InterruptedException e) {                 e.printStackTrace();             }             System.out.println("任務(wù)2結(jié)束");             return 3;         });          CompletableFuture<Integer> completableFuture = cf1.thenCombine(cf2, (result1, result2) -> result1 * result2);         System.out.println("計(jì)算結(jié)果:" + completableFuture.get());     }  }

輸出:

可以看到兩個(gè)任務(wù)確實(shí)是同時(shí)執(zhí)行的

當(dāng)然,熟練了之后,直接使用鏈?zhǔn)讲僮?,代碼如下:

package com.qcy.testCompleteableFuture;  import java.util.concurrent.CompletableFuture;  /**  * @author qcy  * @create 2020/09/07 17:40:44  */ public class Case6 {      public static void main(String[] args) throws Exception {          CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {             System.out.println("任務(wù)1開始");             try {                 Thread.sleep(3000);             } catch (InterruptedException e) {                 e.printStackTrace();             }             System.out.println("任務(wù)1結(jié)束");             return 2;         }).thenCombine(CompletableFuture.supplyAsync(() -> {             System.out.println("任務(wù)2開始");             try {                 Thread.sleep(2000);             } catch (InterruptedException e) {                 e.printStackTrace();             }             System.out.println("任務(wù)2結(jié)束");             return 3;         }), (result1, result2) -> result1 * result2);          System.out.println("計(jì)算結(jié)果:" + completableFuture.get());     }  }

5. 任務(wù)同時(shí)執(zhí)行,且只取最先完成的那個(gè)任務(wù)

public <U> CompletableFuture<U> applyToEither(CompletionStage<? extends T> other, Function<? super T, U> fn);     public CompletableFuture<Void> acceptEither(CompletionStage<? extends T> other, Consumer<? super T> action);     public CompletableFuture<Void> runAfterEither(CompletionStage<?> other,Runnable action);     public static CompletableFuture<Object> anyOf(CompletableFuture<?>... cfs);

applyToEither,最新執(zhí)行完任務(wù),將其結(jié)果執(zhí)行Function操作,其中T是最先執(zhí)行完的任務(wù)結(jié)果類型,U是最后輸出的類型

acceptEither,最新執(zhí)行完的任務(wù),將其結(jié)果執(zhí)行消費(fèi)操作

runAfterEither,任意一個(gè)任務(wù)執(zhí)行完成之后,執(zhí)行Runnable操作

anyOf,多個(gè)任務(wù)中,返回最先執(zhí)行完成的CompletableFuture

5.1 示例:兩個(gè)任務(wù)同時(shí)執(zhí)行,打印最先完成的任務(wù)的結(jié)果

package com.qcy.testCompleteableFuture;  import java.util.concurrent.CompletableFuture;  /**  * @author qcy  * @create 2020/09/07 17:40:44  */ public class Case7 {      public static void main(String[] args) throws Exception {          CompletableFuture<Void> completableFuture = CompletableFuture.supplyAsync(() -> {             System.out.println("任務(wù)1開始");             try {                 Thread.sleep(3000);             } catch (InterruptedException e) {                 e.printStackTrace();             }             System.out.println("任務(wù)1結(jié)束");             return 2;         }).acceptEither(CompletableFuture.supplyAsync(() -> {             System.out.println("任務(wù)2開始");             try {                 Thread.sleep(2000);             } catch (InterruptedException e) {                 e.printStackTrace();             }             System.out.println("任務(wù)2結(jié)束");             return 3;         }), result -> System.out.println(result));          //等待CompletableFuture返回,防止主線程退出         completableFuture.join();     }  }

輸出:

怎么使用CompletableFuture

可以看得到,任務(wù)2結(jié)束后,直接不再執(zhí)行任務(wù)1的剩余代碼

5.2 示例:多個(gè)任務(wù)同時(shí)執(zhí)行,打印最先完成的任務(wù)的結(jié)果

package com.qcy.testCompleteableFuture;  import java.util.concurrent.CompletableFuture;  /**  * @author qcy  * @create 2020/09/07 17:40:44  */ public class Case8 {      public static void main(String[] args) throws Exception {          CompletableFuture<Integer> cf1 = CompletableFuture.supplyAsync(() -> {             System.out.println("任務(wù)1開始");             try {                 Thread.sleep(3000);             } catch (InterruptedException e) {                 e.printStackTrace();             }             System.out.println("任務(wù)1結(jié)束");             return 2;         });          CompletableFuture<Integer> cf2 = CompletableFuture.supplyAsync(() -> {             System.out.println("任務(wù)2開始");             try {                 Thread.sleep(2000);             } catch (InterruptedException e) {                 e.printStackTrace();             }             System.out.println("任務(wù)2結(jié)束");             return 3;         });          CompletableFuture<Integer> cf3 = CompletableFuture.supplyAsync(() -> {             System.out.println("任務(wù)3開始");             try {                 Thread.sleep(4000);             } catch (InterruptedException e) {                 e.printStackTrace();             }             System.out.println("任務(wù)3結(jié)束");             return 4;         });          CompletableFuture<Object> firstCf = CompletableFuture.anyOf(cf1, cf2, cf3);         System.out.println(firstCf.get());     }  }

輸出:

怎么使用CompletableFuture

感謝各位的閱讀,以上就是“怎么使用CompletableFuture”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)怎么使用CompletableFuture這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

網(wǎng)頁題目:怎么使用CompletableFuture
網(wǎng)站地址:http://www.ekvhdxd.cn/article18/jiehdp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、網(wǎng)站改版外貿(mào)建站、微信小程序靜態(tài)網(wǎng)站、網(wǎng)站營銷

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化