日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術文章
文章詳情頁

Java CompletableFuture的使用詳解

瀏覽:165日期:2022-08-15 14:58:27
CompletableFuture​

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

任務開啟

supplyAsync 開啟一個子線程去執行有返回結果

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

例如:

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

返回結果:

Java CompletableFuture的使用詳解

runAsync 開啟一個子線程去執行無結果

任務結束

getjoin 獲得返回值

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);串行任務thenApplythenApplyAsync 串行將異步結果進行同步異步的處理

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

CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> 16 / 2).thenApply(t1 -> t1*2);Assertions.assertEquals(future.join(),16);handlehandleAsync 允許有異常的情況下任然進行異步任務執行

​ handle方法和 thenApply方法處理方式基本一樣。不同的是 handle是在任務完成后再執行,還可以處理異常的任務。thenApply只可以執行正常的任務,任務出現異常則不執行 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);thenAcceptthenAcceptAsync 同步異步穿行消費前任務無返回結果

CompletableFuture<Void> future = CompletableFuture.supplyAsync(() -> getRemoteUser(familyName)).thenAccept(list -> list.forEach(System.out::println));System.out.println(String.format('總執行耗時[%d]毫秒', stopwatch.elapsed(TimeUnit.MILLISECONDS)));future.join();thenRunthenRunAsync 不關注前任務的執行結果

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

CompletableFuture future = CompletableFuture.supplyAsync(() -> 12 / 1).thenRun(() -> System.out.println('無返回值的執行')); System.out.println(future.join());thenComposethenComposeAsync 允許多個任務Future流水線執行

​ 允許你對兩個任務進行流水線操作,第一個操作完成時,將其結果作為參數傳遞給第二個操作。你可以將多個任務嵌套的進行見例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()+'!開吃');

返回結果:

Java CompletableFuture的使用詳解

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

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

whenCompletewhenCompleteAsync 串行將異步結果進行同步異步的處理

​ 與thenAccept很像,區別在于whenComplete的執行會將前任務的返回結果給返回而thenAccept無返回結果。

//whenCompleteCompletableFuture<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);//thenAcceptCompletableFuture<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());//thenApplyCompletableFuture<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 并列多任務執行并結果匯總​

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

例如:

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()+'!開吃');

返回結果:

Java CompletableFuture的使用詳解

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

thenAcceptBoththenAcceptBothAsync 并列多任務執行并消費結果無返回值

與thenCombine差不多,區別是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();applyToEitherapplyToEitherAsync 兩個任務并行進行用快的那個的結果作為后續處理

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

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 兩個任務都完成了不關注執行結果的進行下一步操作

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的使用詳解的詳細內容,更多關于Java CompletableFuture的資料請關注好吧啦網其它相關文章!

標簽: Java
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
99视频在线精品国自产拍免费观看| 国产精品腿扒开做爽爽爽挤奶网站| 香蕉成人久久| 激情欧美国产欧美| 九九综合九九| 国产精品试看| 尤物精品在线| 丝袜脚交一区二区| 日本综合视频| 日韩高清三区| 国产精品大片免费观看| 欧美成人精品一级| 精品视频高潮| 中文字幕成在线观看| 亚洲伦乱视频| 日韩午夜在线| 日本在线不卡视频一二三区| 91精品一区| 蜜桃久久久久| 精品三级久久| 国产亚洲网站| 日韩国产在线观看| 久久99性xxx老妇胖精品| 国内精品美女在线观看| 欧洲一级精品| 99pao成人国产永久免费视频| 蜜臀久久99精品久久久画质超高清 | 日韩一区自拍| 欧美粗暴jizz性欧美20| 免费高清在线一区| 欧美激情日韩| 天堂资源在线亚洲| 蜜桃视频一区二区三区在线观看| 日本午夜精品| 色综合五月天| 狠狠色综合网| 911精品国产| 中文字幕高清在线播放| 亚洲欧美日韩专区| 国产精品**亚洲精品| 青青久久av| 热久久国产精品| 国产精品天天看天天狠| 欧美日韩视频免费观看| 亚洲日产国产精品| 成人台湾亚洲精品一区二区| 1024精品久久久久久久久| 天堂va在线高清一区| 国产一区二区三区四区五区| 午夜在线精品| 精品亚洲精品| 蜜桃视频一区二区三区在线观看| 久久97久久97精品免视看秋霞| 好看的av在线不卡观看| 久久99性xxx老妇胖精品| 亚洲免费成人| 精品国产18久久久久久二百| 尤物网精品视频| 国产精品3区| 日韩午夜免费| 久久97久久97精品免视看秋霞| 亚洲精品一区二区妖精| 欧美一级二区| 99久久99视频只有精品| 欧美日韩一区二区国产| 91精品一区二区三区综合| 日本99精品| 亚洲先锋成人| 国产高清亚洲| 日韩影院免费视频| 不卡专区在线| 青青草伊人久久| 在线日韩av| 国产精品xxx| 久热综合在线亚洲精品| 国产一区二区三区91| 亚州av一区| 日韩在线精品| 国产精品色婷婷在线观看| 欧美日韩视频一区二区三区| 国产一区二区三区探花| 日本少妇精品亚洲第一区| 影音先锋国产精品| 亚洲天堂资源| 国产精品嫩模av在线| 美女精品网站| 久久久久久美女精品| 老司机免费视频一区二区| 亚洲v天堂v手机在线| 国产一区欧美| 日韩久久一区二区三区| 久久99青青| 日韩精品一区二区三区av | 亚洲涩涩av| 一区二区小说| 国产精品久久久久av电视剧| 卡一精品卡二卡三网站乱码| 欧美一区自拍| 午夜久久av| 亚洲视频www| 免费观看久久av| 欧洲在线一区| 精品亚洲美女网站| 亚洲精品**中文毛片| 精品国产成人| 欧美一区二区三区免费看| 亚洲精品在线国产| 免费精品视频最新在线| 国产精品日韩久久久| 国产综合激情| 人人香蕉久久| 久久久久中文| 午夜精品久久久久久久久久蜜桃| 精品视频久久| 精品72久久久久中文字幕| 久久av网址| 欧美黑人巨大videos精品| 久久不见久久见中文字幕免费| 国产精一区二区| 日韩成人精品一区二区三区| 亚洲综合专区| 亚洲精品在线a| 日韩不卡在线观看日韩不卡视频| 日本不卡的三区四区五区| 蜜桃视频在线观看一区二区| 一区二区日韩免费看| 亚洲毛片网站| 日本一区二区三区视频在线看| 日韩一区欧美二区| 蜜桃av一区二区在线观看| 亚洲尤物av| 日韩高清中文字幕一区| 国产日韩1区| 久久精品福利| 女生影院久久| 久久国产主播| 欧美在线资源| 亚洲丝袜美腿一区| 欧美日韩一区二区三区不卡视频| 久久不见久久见免费视频7| 国产一区国产二区国产三区 | 日韩黄色av| 国产精品s色| 樱桃视频成人在线观看| 亚洲四虎影院| 亚洲一区区二区| 日韩av电影一区| 国产精品一区2区3区| 久久精品福利| 久久高清精品| 免费看的黄色欧美网站| 日本综合精品一区| 国产精品chinese| 日韩欧美看国产| aⅴ色国产欧美| 777久久精品| 国产激情在线播放| 国产毛片一区| 久久av影视| 国产精品av一区二区| 午夜久久av | jizzjizz中国精品麻豆| 99精品视频在线观看免费播放| 石原莉奈一区二区三区在线观看| 欧美专区一区| 久久激情网站| 日本综合精品一区| sm捆绑调教国产免费网站在线观看| 五月天久久777| 91麻豆精品| 久久人人97超碰国产公开结果| 91久久在线| 国产精品99久久免费| 在线日韩中文| 国产一区 二区| 欧美国产偷国产精品三区| 91久久久久| 精品视频网站| 天堂成人免费av电影一区| 国产精品igao视频网网址不卡日韩| 99精品国产一区二区三区| 日韩高清电影免费| 久久精品动漫| 久久精品99国产精品日本| 麻豆精品蜜桃| 欧美天堂一区| 国产一区清纯| 免费日韩一区二区三区| 一区二区亚洲精品| 国产福利一区二区精品秒拍| 激情综合自拍| 国产精品xxx在线观看| 91精品久久久久久久久久不卡| 亚洲区第一页| 久久男女视频| 欧美国产极品| 亚洲在线久久| 国产一区二区中文| 国产aⅴ精品一区二区四区| 综合一区二区三区|