欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java CompletableFuture的使用詳解

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

CompletableFuture​

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

任務(wù)開(kāi)啟

supplyAsync 開(kāi)啟一個(gè)子線(xiàn)程去執(zhí)行有返回結(jié)果

開(kāi)啟一個(gè)子線(xiàn)程用來(lái)執(zhí)行執(zhí)行事務(wù),可以通過(guò)返回值的join來(lái)得到返回值.

例如:

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

返回結(jié)果:

runAsync 開(kāi)啟一個(gè)子線(xiàn)程去執(zhí)行無(wú)結(jié)果

任務(wù)結(jié)束

get\join 獲得返回值

join 隱性?huà)伋霎惓?、get顯性?huà)伋霎惓?/p>

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);

串行任務(wù)

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

​ 在當(dāng)前階段正常執(zhí)行完成后(正常執(zhí)行是指沒(méi)有拋出異常)對(duì)前者的結(jié)果進(jìn)行的操作。

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

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

handle方法和 thenApply方法處理方式基本一樣。不同的是 handle是在任務(wù)完成后再執(zhí)行,還可以處理異常的任務(wù)。thenApply只可以執(zhí)行正常的任務(wù),任務(wù)出現(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 同步\異步穿行消費(fèi)前任務(wù)無(wú)返回結(jié)果

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

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

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

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

thenCompose\thenComposeAsync 允許多個(gè)任務(wù)Future流水線(xiàn)執(zhí)行

​ 允許你對(duì)兩個(gè)任務(wù)進(jìn)行流水線(xiàn)操作,第一個(gè)操作完成時(shí),將其結(jié)果作為參數(shù)傳遞給第二個(gè)操作。你可以將多個(gè)任務(wù)嵌套的進(jìn)行見(jiàn)例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()+"!開(kāi)吃");

返回結(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將上一個(gè)異步的結(jié)果(文中的rice)拿到以后通過(guò)一個(gè)線(xiàn)程去執(zhí)行了當(dāng)前異步任務(wù),并將結(jié)果在future.join()中輸出了。

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

​ 與thenAccept很像,區(qū)別在于whenComplete的執(zhí)行會(huì)將前任務(wù)的返回結(jié)果給返回而thenAccept無(wú)返回結(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-------");

并行任務(wù)

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

同時(shí)執(zhí)行兩個(gè)異步任務(wù),并且在最后通過(guò)BiFunction將兩個(gè)結(jié)果綜合起來(lái)進(jìn)行結(jié)果輸出.

例如:

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

返回結(jié)果:

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

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

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

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 兩個(gè)任務(wù)并行進(jìn)行用快的那個(gè)的結(jié)果作為后續(xù)處理

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

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 兩個(gè)任務(wù)都完成了不關(guān)注執(zhí)行結(jié)果的進(jìn)行下一步操作

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的使用詳解的詳細(xì)內(nèi)容,更多關(guān)于Java CompletableFuture的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Eclipse搭建spring開(kāi)發(fā)環(huán)境圖文教程(推薦)

    Eclipse搭建spring開(kāi)發(fā)環(huán)境圖文教程(推薦)

    下面小編就為大家?guī)?lái)一篇Eclipse搭建spring開(kāi)發(fā)環(huán)境圖文教程(推薦)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-07-07
  • Java游戲開(kāi)發(fā)之俄羅斯方塊的實(shí)現(xiàn)

    Java游戲開(kāi)發(fā)之俄羅斯方塊的實(shí)現(xiàn)

    俄羅斯方塊是一個(gè)最初由阿列克謝帕吉特諾夫在蘇聯(lián)設(shè)計(jì)和編程的益智類(lèi)視頻游戲。本文和大家分享了利用Java語(yǔ)言實(shí)現(xiàn)這一經(jīng)典的小游戲的示例代碼,需要的可以參考一下
    2022-05-05
  • Java設(shè)計(jì)模式之單例模式實(shí)例詳解【懶漢式與餓漢式】

    Java設(shè)計(jì)模式之單例模式實(shí)例詳解【懶漢式與餓漢式】

    這篇文章主要介紹了Java設(shè)計(jì)模式之單例模式,簡(jiǎn)單說(shuō)明了單例模式的原理并結(jié)合具體實(shí)例形式分析了單例模式中懶漢式與餓漢式的具體實(shí)現(xiàn)與使用技巧,需要的朋友可以參考下
    2017-09-09
  • Spring IOC 三種配置方式詳解

    Spring IOC 三種配置方式詳解

    這篇文章主要介紹了Spring IOC 三種配置方式,基于xml配置方式組件管理,基于注解方式管理和配置類(lèi)方式管理,這三種方式,通過(guò)圖文講解的非常詳細(xì),需要的朋友可以參考下
    2024-05-05
  • java旋轉(zhuǎn)二維數(shù)組實(shí)例

    java旋轉(zhuǎn)二維數(shù)組實(shí)例

    這篇文章主要介紹了java旋轉(zhuǎn)二維數(shù)組,以實(shí)例形式較為詳細(xì)的講述了旋轉(zhuǎn)二維數(shù)的原理與實(shí)現(xiàn)方法,需要的朋友可以參考下
    2014-10-10
  • Java 面試題和答案 - (下)

    Java 面試題和答案 - (下)

    本文主要介紹Java 面試題,這里整理了Java面試題關(guān)于JDBC,線(xiàn)程異常處理,Servlet,JSP的知識(shí)的整理,幫助大家理解知識(shí)點(diǎn),便于面試,有興趣的小伙伴可以參考下
    2016-09-09
  • 淺談SpringMVC請(qǐng)求映射handler源碼解讀

    淺談SpringMVC請(qǐng)求映射handler源碼解讀

    這篇文章主要介紹了淺談SpringMVC請(qǐng)求映射handler源碼解讀,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • Java視頻斷點(diǎn)上傳的實(shí)現(xiàn)示例

    Java視頻斷點(diǎn)上傳的實(shí)現(xiàn)示例

    斷點(diǎn)續(xù)傳指的是在下載或上傳時(shí),將下載或上傳任務(wù)人為的劃分為幾個(gè)部分,本文主要介紹了Java視頻斷點(diǎn)上傳的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-05-05
  • MyEclipse到期破解代碼分享

    MyEclipse到期破解代碼分享

    前幾天有個(gè)小伙伴咨詢(xún),使用的時(shí)候一直說(shuō)myeclipse已過(guò)期,需要購(gòu)買(mǎi),如何解決?可以去網(wǎng)上搜搜注冊(cè)碼,但作為程序猿這么做簡(jiǎn)直太無(wú)趣,看看我們自己來(lái)解決這個(gè)問(wèn)題
    2014-11-11
  • @Autowired 自動(dòng)注入接口失敗的原因及解決

    @Autowired 自動(dòng)注入接口失敗的原因及解決

    這篇文章主要介紹了@Autowired 自動(dòng)注入接口失敗的原因及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02

最新評(píng)論