您好,登錄后才能下訂單哦!
這篇文章主要介紹“分析Java8使用工廠方法supplyAsync創(chuàng)建CompletableFuture”,在日常操作中,相信很多人在分析Java8使用工廠方法supplyAsync創(chuàng)建CompletableFuture問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”分析Java8使用工廠方法supplyAsync創(chuàng)建CompletableFuture”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
采用 supplyAsync 方法后,可以用一行代碼重寫getPriceAsync 方法。
【使用工廠方法 supplyAsync 創(chuàng)建 CompletableFuture 對象】
public Future<Double> getPriceAsync(String product) { return CompletableFuture.supplyAsync(() -> calculatePrice(product)); }
supplyAsync 方法接受一個生產(chǎn)者( Supplier )作為參數(shù),返回一個 CompletableFuture對象,該對象完成異步執(zhí)行后會讀取調(diào)用生產(chǎn)者方法的返回值。
生產(chǎn)者方法會交由 ForkJoinPool池中的某個執(zhí)行線程( Executor )運行,但是你也可以使用 supplyAsync 方法的重載版本,傳遞第二個參數(shù)指定不同的執(zhí)行線程執(zhí)行生產(chǎn)者方法。
一般而言,向 CompletableFuture 的工廠方法傳遞可選參數(shù),指定生產(chǎn)者方法的執(zhí)行線程是可行的,后面我們會會介紹如何使用適合你應用特性的執(zhí)行線程改善程序的性能。
剛剛的代碼
public Future<Double> getPriceAsync(String product) { return CompletableFuture.supplyAsync(() -> calculatePrice(product)); }
getPriceAsync 方法返回的 CompletableFuture 對象和 下面的代碼
public Future<Double> getPriceAsync(String product) { CompletableFuture<Double> futurePrice = new CompletableFuture<>(); new Thread( () -> { try { double price = calculatePrice(product); futurePrice.complete(price); } catch (Exception ex) { futurePrice.completeExceptionally(ex); } }).start(); return futurePrice; }
手工創(chuàng)建和完成的 CompletableFuture 對象是完全等價的,這意味著它提供了同樣的錯誤管理機制,而前者你花費了大量的精力才得以構建。
驗證代碼如下
ExecutorService executorService = Executors.newFixedThreadPool(3); //executorService.submit(new RuleTestRunnable(1)); List<Integer> taskList = new ArrayList<>(); for (int i = 0; i < 30; i++) { taskList.add(i); } CompletableFuture<String> a1 = CompletableFuture.supplyAsync(() -> { logger.info("線程1{}{}","開始"); try { TimeUnit.MILLISECONDS.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } logger.info("線程1{}{}","結束"); return "1"; },executorService); CompletableFuture<String> a2 = CompletableFuture.supplyAsync(() -> { logger.info("線程2{}{}","開始"); try { TimeUnit.MILLISECONDS.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } logger.info("線程2{}{}","結束"); return "1"; },executorService); CompletableFuture<Object> a= a1.thenCombineAsync(a2,(s1,s2) -> { logger.info("組合線程{}{}"); return s1+s2; },executorService); Object result = a.get();
當executorService線程池大小為2時候,執(zhí)行結果如下:
[pool-4-thread-1] INFO test.rcd.thread.CompletableFutureDemo.lambda$mains$4:127 - 組合線程{}{}
a1.thenCombineAsync方法始終被線程1或2執(zhí)行
當executorService線程池大小為3時候,執(zhí)行結果如下:
[pool-4-thread-3] INFO test.rcd.thread.CompletableFutureDemo.lambda$mains$4:127 - 組合線程{}{}
a1.thenCombineAsync方法始終被線程3執(zhí)行
改為a1.thenCombine(),執(zhí)行結果:
a1.thenCombineAsync方法始終被線程1或2執(zhí)行
由此可見,async方法始終嘗試取新線程執(zhí)行方法,不帶async方法則會從當前線程里取線程執(zhí)行.CompletableFuture似是與線程無關的。
到此,關于“分析Java8使用工廠方法supplyAsync創(chuàng)建CompletableFuture”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關知識,請繼續(xù)關注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。