最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java CompletableFuture的使用詳解

 更新時間:2021年03月10日 09:00:07   作者:Mirrors  
這篇文章主要介紹了Java CompletableFuture的使用詳解,幫助大家更好的理解和學習使用Java,感興趣的朋友可以了解下

CompletableFuture​

它代表某個同步或異步計算的一個階段。你可以把它理解為是一個為了產(chǎn)生有價值最終結(jié)果的計算的流水線上的一個單元。這意味著多個指令可以鏈接起來從而一個階段的完成可以觸發(fā)下一個階段的執(zhí)行。

任務開啟

supplyAsync 開啟一個子線程去執(zhí)行有返回結(jié)果

開啟一個子線程用來執(zhí)行執(zhí)行事務,可以通過返回值的join來得到返回值.

例如:

print("去煮飯了");
CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
 print("煮飯中....");
 sleep();
 sleep();
 sleep();
 print("煮飯完成");
 return "盛米飯";
});
sleep();
print("炒完菜了");
sleep();
print(completableFuture.join()+"!開吃");

返回結(jié)果:

runAsync 開啟一個子線程去執(zhí)行無結(jié)果

任務結(jié)束

get\join 獲得返回值

join 隱性拋出異常、get顯性拋出異常

Stopwatch stopwatch = Stopwatch.createStarted();
CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> 16 / 2);
CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> 27 / 3);
try {
 Assertions.assertEquals(future1.get(),8);
} catch (InterruptedException e) {
 e.printStackTrace();
} catch (ExecutionException e) {
 e.printStackTrace();
}
Assertions.assertEquals(future2.join(),9);

串行任務

thenApply\thenApplyAsync 串行將異步結(jié)果進行同步\異步的處理

​ 在當前階段正常執(zhí)行完成后(正常執(zhí)行是指沒有拋出異常)對前者的結(jié)果進行的操作。

CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> 16 / 2).thenApply(t1 -> t1*2);
Assertions.assertEquals(future.join(),16);

handle\handleAsync 允許有異常的情況下任然進行異步任務執(zhí)行

handle方法和 thenApply方法處理方式基本一樣。不同的是 handle是在任務完成后再執(zhí)行,還可以處理異常的任務。thenApply只可以執(zhí)行正常的任務,任務出現(xiàn)異常則不執(zhí)行 thenApply方法。

CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> 16 / 0).handle((t1, e) -> {
 System.out.println("handle=" + e.getMessage());
 return Integer.MAX_VALUE;
});
Assertions.assertEquals(future.join(),Integer.MAX_VALUE);

thenAccept\thenAcceptAsync 同步\異步穿行消費前任務無返回結(jié)果

CompletableFuture<Void> future = CompletableFuture.supplyAsync(() -> getRemoteUser(familyName))
	.thenAccept(list -> list.forEach(System.out::println));
System.out.println(String.format("總執(zhí)行耗時[%d]毫秒", stopwatch.elapsed(TimeUnit.MILLISECONDS)));
future.join();

thenRun\thenRunAsync 不關注前任務的執(zhí)行結(jié)果

不關心任務的處理結(jié)果。只要上面的任務正確的執(zhí)行完成,就開始執(zhí)行。同樣其也無返回值

 CompletableFuture future = CompletableFuture.supplyAsync(() -> 12 / 1).thenRun(() -> System.out.println("無返回值的執(zhí)行"));
 System.out.println(future.join());

thenCompose\thenComposeAsync 允許多個任務Future流水線執(zhí)行

​ 允許你對兩個任務進行流水線操作,第一個操作完成時,將其結(jié)果作為參數(shù)傳遞給第二個操作。你可以將多個任務嵌套的進行見例2

例1:

print("去煮飯了");
CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
 print("煮飯中....");
 sleep();
 sleep();
 print("煮飯完成");
 return "米飯";
}).thenCompose(rice -> CompletableFuture.supplyAsync(() ->{
 print("洗碗");
 sleep();
 print("洗碗洗完了");
 return rice+"盛好了";
}));
sleep();
print("炒完菜了");
print(completableFuture.join()+"!開吃");

返回結(jié)果:

例2:

CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> 16 / 2)
 .thenComposeAsync(t1 -> CompletableFuture.supplyAsync(() -> t1 / 2)
   .thenComposeAsync(t2 -> CompletableFuture.supplyAsync(() -> t2 / 2)));
Assertions.assertEquals(future.join(),2);

結(jié)論:可以看出supplyAsync執(zhí)行了異步方法,thenCompose將上一個異步的結(jié)果(文中的rice)拿到以后通過一個線程去執(zhí)行了當前異步任務,并將結(jié)果在future.join()中輸出了。

whenComplete\whenCompleteAsync 串行將異步結(jié)果進行同步\異步的處理

​ 與thenAccept很像,區(qū)別在于whenComplete的執(zhí)行會將前任務的返回結(jié)果給返回而thenAccept無返回結(jié)果。

//whenComplete
CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> 16 / 2);
CompletableFuture<Integer> future = future1.whenComplete((t1, e) -> {
 Assertions.assertEquals(Thread.currentThread().getName(),"main");
 Assertions.assertEquals(t1, 8);
 Assertions.assertNull(e);
 t1 = 10;
});
Assertions.assertEquals(future.join(), 8);
//thenAccept
CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> 16 / 2);
CompletableFuture<Void> future3 = future2.thenAccept(t1 -> {
 Assertions.assertEquals(Thread.currentThread().getName(), "main");
 Assertions.assertEquals(t1, 8);
});
Assertions.assertNull(future3.join());
//thenApply
CompletableFuture<Integer> future4 = CompletableFuture.supplyAsync(() -> 16 / 2);
CompletableFuture<Integer> future5 = future4.thenApply(t1 -> {
 Assertions.assertEquals(Thread.currentThread().getName(), "main");
 Assertions.assertEquals(t1, 8);
 return t1*2;
});
Assertions.assertEquals(future5.join(),16);
System.out.println("------OK-------");

并行任務

thenCombine 并列多任務執(zhí)行并結(jié)果匯總​

同時執(zhí)行兩個異步任務,并且在最后通過BiFunction將兩個結(jié)果綜合起來進行結(jié)果輸出.

例如:

print("去煮飯了");
CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
 print("煮飯中....");
 sleep();
 sleep();
 print("煮飯完成");
 return "米飯";
}).thenCombine(CompletableFuture.supplyAsync(() ->{
 print("洗碗");
 sleep();
 print("洗碗洗完了");
 return "碗好了";
}),(rice,bowl) -> {
 print("盛個飯");
 return "盛個飯";
});
sleep();
print("炒完菜了");
print(completableFuture.join()+"!開吃");

返回結(jié)果:

結(jié)論:可以看出supplyAsync執(zhí)行了異步方法,thenCombine又起了一個新的線程并把兩者的結(jié)果綜合到一起(rice/bowl),由BiFunction進行計算,并將結(jié)果在future.join()中輸出了。

thenAcceptBoth\thenAcceptBothAsync 并列多任務執(zhí)行并消費結(jié)果無返回值

thenCombine差不多,區(qū)別是thenAcceptBoth無返回值

CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> 16 / 2).thenApply(t1 -> t1/2);
CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> 27 / 3).thenApply(t1 -> t1/3);
CompletableFuture<Void> completableFuture = future1.thenAcceptBoth(future2, (t1, t2) -> {
 Assertions.assertEquals(t1 + t2, 7);
});
completableFuture.join();

applyToEither\applyToEitherAsync 兩個任務并行進行用快的那個的結(jié)果作為后續(xù)處理

​ 兩個任務,誰執(zhí)行返回的結(jié)果快,我就用那個任務的結(jié)果進行下一步的操作。

Stopwatch stopwatch = Stopwatch.createStarted();
CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> {
 sleep(Integer.MAX_VALUE);
 return 16 / 2;
});
CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> 27 / 3);
CompletableFuture<Integer> future = future1.applyToEither(future2, t -> t);
Assertions.assertEquals(future.join(),9);
Assertions.assertTrue(stopwatch.elapsed(TimeUnit.MILLISECONDS) < 1000);

runAfterBoth/runAfterBothAsync 兩個任務都完成了不關注執(zhí)行結(jié)果的進行下一步操作

Stopwatch stopwatch = Stopwatch.createStarted();
CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> {
 sleep(2000);
 return 16 / 2;
});
CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> 27 / 3);
CompletableFuture<Void> future = future1.runAfterBothAsync(future2,() -> System.out.println("1234"));
future.join();
Assertions.assertTrue(stopwatch.elapsed(TimeUnit.MILLISECONDS) > 2000);

以上就是Java CompletableFuture的使用詳解的詳細內(nèi)容,更多關于Java CompletableFuture的資料請關注腳本之家其它相關文章!

相關文章

  • SpringBoot+MinIO實現(xiàn)對象存儲的示例詳解

    SpringBoot+MinIO實現(xiàn)對象存儲的示例詳解

    MinIO?是一個基于Apache?License?v2.0開源協(xié)議的對象存儲服務,它是一個非常輕量的服務,可以很簡單的和其他應用的結(jié)合,所以下面我們就來看看SpringBoot如何整合MinIO實現(xiàn)對象存儲吧
    2023-10-10
  • JVM?jstack實戰(zhàn)之死鎖問題詳解

    JVM?jstack實戰(zhàn)之死鎖問題詳解

    如果在生產(chǎn)環(huán)境發(fā)生了死鎖,我們將看到的是部署的程序沒有任何反應了,這個時候我們可以借助?jstack進行分析,下面我們實戰(zhàn)操作查找死鎖的原因
    2022-10-10
  • Java中String字符串轉(zhuǎn)具體對象的幾種常用方式

    Java中String字符串轉(zhuǎn)具體對象的幾種常用方式

    String對象可以用來存儲任何字符串類型的數(shù)據(jù),包括HTML、XML等格式的字符串,下面這篇文章主要給大家介紹了關于JavaString字符串轉(zhuǎn)具體對象的幾種常用方式,需要的朋友可以參考下
    2024-03-03
  • Idea打不了斷點如何解決

    Idea打不了斷點如何解決

    這篇文章主要介紹了Idea打不了斷點如何解決的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • java開發(fā)之內(nèi)部類的用法

    java開發(fā)之內(nèi)部類的用法

    本篇文章介紹了,java開發(fā)之內(nèi)部類的用法。需要的朋友參考下
    2013-05-05
  • Java中的注解、元注解詳細解析

    Java中的注解、元注解詳細解析

    這篇文章主要介紹了Java中的注解、元注解詳細解析,注解也叫元數(shù)據(jù),與類、接口、枚舉是在同一個層次,它可以聲明在包、類、字段、方法、局部變量、方法參數(shù)等的前面,用來對這些元素進行說明,注釋,需要的朋友可以參考下
    2023-11-11
  • 聊聊為什么要使用BufferedReader讀取File

    聊聊為什么要使用BufferedReader讀取File

    這篇文章主要介紹了為什么要使用BufferedReader讀取File,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • spring boot切面execution表達式添加多個包路徑問題及解決方案

    spring boot切面execution表達式添加多個包路徑問題及解決方案

    在Spring Boot中,如果你想為多個包中的方法創(chuàng)建一個切面,你可以在@Pointcut注解中使用||操作符來指定多個包,下面給大家分享spring boot切面execution表達式添加多個包路徑問題及解決方案,感興趣的朋友跟隨小編一起看看吧
    2024-03-03
  • Java輕松實現(xiàn)表單提交的三種方法

    Java輕松實現(xiàn)表單提交的三種方法

    在Web開發(fā)中,表單是用戶與網(wǎng)站交互的主要方式之一,本文將詳細介紹如何在Java中實現(xiàn)表單提交,并通過代碼和案例為新手朋友提供詳細的指導,有需要的可以參考下
    2024-10-10
  • Spring注解@Import原理解析

    Spring注解@Import原理解析

    這篇文章主要為大家介紹了Spring注解@Import原理解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-02-02

最新評論

合川市| 南昌市| 祁阳县| 长岭县| 怀来县| 镇远县| 察隅县| 金乡县| 张家界市| 伊川县| 临江市| 白水县| 汉川市| 固阳县| 桃江县| 大化| 建宁县| 城固县| 清流县| 越西县| 武川县| 县级市| 丹阳市| 临颍县| 旅游| 南陵县| 都江堰市| 繁峙县| 潮安县| 久治县| 古浪县| 三都| 前郭尔| 保亭| 莎车县| 澎湖县| 高清| 佛教| 安徽省| 东山县| 方正县|