詳解Java CompletableFuture使用方法以及與FutureTask的區(qū)別
總的來說簡潔了FutureTask與線程池的配合使用
沒啥太大區(qū)別吧我覺得, 使用方法不一樣, 多了一些方法 ???
futureTask 創(chuàng)建異步任務(wù)
FutureTask<String> stringFutureTask = new FutureTask<>(() -> { return "aa"; }); executorService.execute(stringFutureTask); System.out.println(stringFutureTask.get()); CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> { return "aa"; }, executorService); // 不用手動提交了 System.out.println(future1.get());
還有很多異步回調(diào), 組合處理
創(chuàng)建任務(wù)
1. .supplyAsync
創(chuàng)建一個帶返回值的任務(wù)
2. .runAsync
創(chuàng)建一個不帶返回值的任務(wù)
ExecutorService executorService = Executors.newFixedThreadPool(1); // 帶返回值 CompletableFuture future = CompletableFuture.supplyAsync(() -> { try { System.out.println("future " + new Date()); Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } return "aaa"; }, executorService); // 推薦使用
以上兩個方法都有兩個構(gòu)造方法, 默認(rèn)不指定自定義線程池, 他會指定默認(rèn)的提交任務(wù)的方法
// 查看cpu的核數(shù)是否大于1核 private static final boolean useCommonPool = (ForkJoinPool.getCommonPoolParallelism() > 1); // 如果大于1核 則調(diào)用execute方法, 每次創(chuàng)建一個線程 private static final Executor asyncPool = useCommonPool ? ForkJoinPool.commonPool() : new ThreadPerTaskExecutor(); static final class ThreadPerTaskExecutor implements Executor { public void execute(Runnable r) { new Thread(r).start(); } }
所以推薦自定義線程池的方式
異步回調(diào)
指的是 異步任務(wù)結(jié)束后調(diào)用的任務(wù)
1. .thenApply
帶返回值的異步調(diào)用函數(shù), 有入?yún)? 有出參
2. .thenAccept
不帶返回值的異步回調(diào)函數(shù), 有入?yún)?/p>
CompletableFuture future = CompletableFuture.supplyAsync(() -> { try { System.out.println("future " + new Date()); Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } return "aaa"; }, executorService); // future執(zhí)行完之后執(zhí)行的異步任務(wù) CompletableFuture<String> thenApply = future.thenApply((result) -> { System.out.println("future2 " +new Date()); System.out.println(result); return "bbb" + result; });
3. .exceptionally
異步任務(wù)出現(xiàn)異常調(diào)用的回調(diào)方法
CompletableFuture future = CompletableFuture.supplyAsync(() -> { try { System.out.println("future " + new Date()); Thread.sleep(2000); int a = 1 / 0; } catch (InterruptedException e) { e.printStackTrace(); } return "aaa"; }, executorService); CompletableFuture<String> exceptionally = future.exceptionally((result) -> { System.out.println("future3 " + result); return "bbb" + result; }); // 出現(xiàn)異常則返回異常, 沒異常則返回future的返回值 System.out.println(exceptionally.get());
去掉異常
4. .whenComplete
當(dāng)主任務(wù)出現(xiàn)異常時, 會終止任務(wù),get的時候會拋出主任務(wù)的異常, 入?yún)⒅禐閚ull, 否則正常運(yùn)行
CompletableFuture future = CompletableFuture.supplyAsync(() -> { try { System.out.println("future " + new Date()); Thread.sleep(2000); int a = 1/0; } catch (InterruptedException e) { e.printStackTrace(); } return "aaa"; }, executorService); CompletableFuture<String> exceptionally = future.whenComplete((result, error) -> { System.out.println("future3 " + result); System.out.println("future3 " + error); }); System.out.println(exceptionally.get());
去掉異常
組合處理
....
就是將多個任務(wù)組合起來執(zhí)行, 時間原因, 這里我就不介紹了, 大家另行百度吧
到此這篇關(guān)于詳解Java CompletableFuture使用方法的文章就介紹到這了,更多相關(guān)Java CompletableFuture內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實(shí)現(xiàn)常用加密算法——單向加密算法MD5和SHA
本篇文章主要介紹了Java實(shí)現(xiàn)常用加密算法——單向加密算法MD5和SHA,信息加密后數(shù)據(jù)更安全,需要的朋友可以參考下。2016-10-10JAVAEE中用Session簡單實(shí)現(xiàn)購物車功能示例代碼
本篇文章主要介紹了JAVAEE中用Session簡單實(shí)現(xiàn)購物車功能示例代碼,非常具有實(shí)用價值,需要的朋友可以參考下。2017-03-03Spring實(shí)戰(zhàn)之緩存使用key操作示例
這篇文章主要介紹了Spring實(shí)戰(zhàn)之緩存使用key操作,結(jié)合實(shí)例形式分析了Spring緩存使用key具體配置、屬性、領(lǐng)域模型等相關(guān)操作技巧,需要的朋友可以參考下2020-01-01java如何自定義List中的sort()排序,用于日期排序
這篇文章主要介紹了java如何自定義List中的sort()排序,用于日期排序,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11Java并發(fā)編程Callable與Future的應(yīng)用實(shí)例代碼
這篇文章主要介紹了Java并發(fā)編程Callable與Future的應(yīng)用實(shí)例代碼,具有一定借鑒價值,需要的朋友可以參考下2018-01-01