色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

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

Java CompletableFuture的使用詳解

瀏覽:19日期: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
相關文章:
主站蜘蛛池模板: 亚洲最新网站 | 亚洲国产一区二区三区a毛片 | 欧美性一级| 亚洲国产精品乱码在线观看97 | 久久99网站| 男人扒开双腿女人爽视频免费 | 美女美女大片黄a大片 | 九九精品视频在线 | 亚洲国产欧洲精品路线久久 | 久久九九爱 | 99国产精品高清一区二区二区 | 欧美极品在线播放 | 国产99视频精品免费视频免里 | 草草免费观看视频在线 | 九九九九热精品视频 | 黄网在线观看免费 | 在线播放高清国语自产拍免费 | 日韩国产欧美精品综合二区 | 国产三级网| 国产精品亚洲国产三区 | 久久视频精品36线视频在线观看 | 久久精品大片 | 孕妇xxxx视频在线 | 国产网址在线观看 | 中文字幕一区二区三区有限公司 | 韩国一区在线 | 亚洲第一页在线播放 | 日本三级日产三级国产三级 | 亚洲一级毛片视频 | 亚洲欧美日韩国产精品一区 | a级片在线观看视频 | 免费观看欧美一级片 | 久久成年人视频 | 不卡一级aaa全黄毛片 | 男人操美女逼视频 | 成年美女黄网站色大 | 欧美激情精品久久久久久久九九九 | 国产亚洲欧美一区 | 在线观看黄网 | 性欧美videos 精品 | 香港毛片免费观看 |