默認(rèn)使用的線程池
不傳executor時(shí)默認(rèn)使用ForkJoinPool.commonPool()
IntStream.range(0, 15).parallel().forEach(i -> { System.out.println(Thread.currentThread()); });
輸出
Thread[ForkJoinPool.commonPool-worker-1,5,main] Thread[main,5,main] Thread[ForkJoinPool.commonPool-worker-1,5,main] Thread[ForkJoinPool.commonPool-worker-3,5,main] Thread[ForkJoinPool.commonPool-worker-2,5,main] Thread[ForkJoinPool.commonPool-worker-3,5,main] Thread[ForkJoinPool.commonPool-worker-1,5,main] Thread[main,5,main] Thread[ForkJoinPool.commonPool-worker-1,5,main] Thread[ForkJoinPool.commonPool-worker-3,5,main] Thread[ForkJoinPool.commonPool-worker-2,5,main] Thread[ForkJoinPool.commonPool-worker-3,5,main] Thread[ForkJoinPool.commonPool-worker-1,5,main] Thread[main,5,main] Thread[ForkJoinPool.commonPool-worker-2,5,main]
commonPool
thenRun測(cè)試實(shí)例1 不設(shè)定executorThis pool is statically constructed; its run state is unaffected by attempts to shutdown() or shutdownNow(). However this pool and any ongoing processing are automatically terminated upon program System.exit(int). Any program that relies on asynchronous task processing to complete before program termination should invoke commonPool().awaitQuiescence, before exit.
@Test public void testRunOnCommonPool() throws InterruptedException { CompletionStagefuturePrice = CompletableFuture.runAsync(() -> { try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("test1:1 - runAsync(runnable), job thread: " + Thread.currentThread()); //Thread[ForkJoinPool.commonPool-worker-1,5,main] } ); System.out.println("test1:flag1"); futurePrice.thenRun(() -> { System.out.println("test1:2 - thenRun(runnable)), action thread: " + Thread.currentThread()); //Thread[ForkJoinPool.commonPool-worker-1,5,main] }); System.out.println("test1:flag2"); futurePrice.thenRunAsync(() -> { System.out.println("test1:3 - thenRunAsync(runnable), action thread: " + Thread.currentThread()); //Thread[ForkJoinPool.commonPool-worker-1,5,main] }); TimeUnit.SECONDS.sleep(100); }
輸出
test1:flag1 test1:flag2 test1:1 - runAsync(runnable), job thread: Thread[ForkJoinPool.commonPool-worker-1,5,main] test1:2 - thenRun(runnable)), action thread: Thread[ForkJoinPool.commonPool-worker-1,5,main] test1:3 - thenRunAsync(runnable), action thread: Thread[ForkJoinPool.commonPool-worker-1,5,main]設(shè)定executor
@Test public void testRunOnExecutors() throws InterruptedException { CompletionStagefuturePrice = CompletableFuture.runAsync(() -> { try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("test2:1 - runAsync(runnable, executor), job thread: " + Thread.currentThread()); //Thread[pool-1-thread-1,5,main] }, executor); System.out.println("test2:flag1"); futurePrice.thenRunAsync(() -> { System.out.println("test2:2 - thenRunAsync(runnable), action thread: " + Thread.currentThread()); //Thread[pool-1-thread-1,5,main] }); System.out.println("test2:flag2"); futurePrice.thenRun(() -> { System.out.println("test2:3 - thenRun(runnable), action thread: " + Thread.currentThread()); //Thread[pool-1-thread-2,5,main] }); futurePrice.thenRunAsync(() -> { System.out.println("test2:4 - thenRunAsync(runnable, executor), action thread: " + Thread.currentThread()); //Thread[ForkJoinPool.commonPool-worker-1,5,main] }, executor); TimeUnit.SECONDS.sleep(100); }
輸出
test2:flag1 test2:flag2 test2:1 - runAsync(runnable, executor), job thread: Thread[pool-1-thread-1,5,main] test2:3 - thenRun(runnable), action thread: Thread[pool-1-thread-1,5,main] test2:4 - thenRunAsync(runnable, executor), action thread: Thread[pool-1-thread-2,5,main] test2:2 - thenRunAsync(runnable), action thread: Thread[ForkJoinPool.commonPool-worker-1,5,main]thenRun測(cè)試實(shí)例2 沒有sleep
@Test public void testThenRun(){ CompletableFuturef1 = CompletableFuture.supplyAsync(() -> { System.out.println("f1 thread:"+Thread.currentThread().getName()); return "zero"; }, executor); f1.thenRun(new Runnable() { @Override public void run() { System.out.println("then run thread:"+Thread.currentThread().getName()); System.out.println("finished"); } }); TimeUnit.SECONDS.sleep(10); }
使用executor的輸出
f1 thread:pool-1-thread-1 then run thread:main finished
不使用executor的輸出
f1 thread:ForkJoinPool.commonPool-worker-1 then run thread:main finished加上sleep
CompletableFuturef1 = CompletableFuture.supplyAsync(() -> { System.out.println("f1 thread:"+Thread.currentThread().getName()); try { TimeUnit.SECONDS.sleep(2); } catch (InterruptedException e) { e.printStackTrace(); } return "zero"; }, executor); f1.thenRun(new Runnable() { @Override public void run() { System.out.println("then run thread:"+Thread.currentThread().getName()); System.out.println("finished"); } }); TimeUnit.SECONDS.sleep(10);
使用executor的輸出
f1 thread:pool-1-thread-1 then run thread:pool-1-thread-1 finished
不使用executor的輸出
f1 thread:ForkJoinPool.commonPool-worker-1 then run thread:ForkJoinPool.commonPool-worker-1 finished小結(jié)
doc不帶 async 的 thenRun() 方法仍然是一個(gè)異步方法,可能是使用main線程,commonPool的線程或者是executor的線程。
ForkJoinPool.commonPool
理解 CompletableFuture 的任務(wù)與回調(diào)函數(shù)的線程
哪個(gè)線程執(zhí)行 CompletableFuture’s tasks 和 callbacks?
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/66703.html
摘要:方法接受一個(gè)生產(chǎn)者作為參數(shù),返回一個(gè)對(duì)象,該對(duì)象完成異步執(zhí)行后會(huì)讀取調(diào)用生產(chǎn)者方法的返回值。該方法接收一個(gè)對(duì)象構(gòu)成的數(shù)組,返回由第一個(gè)執(zhí)行完畢的對(duì)象的返回值構(gòu)成的。 一、Future 接口 在Future中觸發(fā)那些潛在耗時(shí)的操作把調(diào)用線程解放出來(lái),讓它能繼續(xù)執(zhí)行其他有價(jià)值的工作,不再需要呆呆等待耗時(shí)的操作完成。打個(gè)比方,你可以把它想象成這樣的場(chǎng)景:你拿了一袋子衣服到你中意的干洗店去洗。...
摘要:在這種方式中,主線程不會(huì)被阻塞,不需要一直等到子線程完成。主線程可以并行的執(zhí)行其他任務(wù)。如果我們不想等待結(jié)果返回,我們可以把需要等待完成執(zhí)行的邏輯寫入到回調(diào)函數(shù)中。任何立即執(zhí)行完成那就是執(zhí)行在主線程中嘗試刪除測(cè)試下??梢允褂眠_(dá)成目的。 Java 8 有大量的新特性和增強(qiáng)如 Lambda 表達(dá)式,Streams,CompletableFuture等。在本篇文章中我將詳細(xì)解釋清楚Compl...
摘要:方法接收的是的實(shí)例,但是它沒有返回值方法是函數(shù)式接口,無(wú)參數(shù),會(huì)返回一個(gè)結(jié)果這兩個(gè)方法是的升級(jí),表示讓任務(wù)在指定的線程池中執(zhí)行,不指定的話,通常任務(wù)是在線程池中執(zhí)行的。該的接口是在線程使用舊的接口,它不允許返回值。 簡(jiǎn)介 作為Java 8 Concurrency API改進(jìn)而引入,本文是CompletableFuture類的功能和用例的介紹。同時(shí)在Java 9 也有對(duì)Completab...
摘要:并行流與目前,我們對(duì)集合進(jìn)行計(jì)算有兩種方式并行流而更加的靈活,我們可以配置線程池的大小確保整體的計(jì)算不會(huì)因?yàn)榈却l(fā)生阻塞。 【回顧Future接口 Future接口時(shí)java5引入的,設(shè)計(jì)初衷是對(duì)將來(lái)某個(gè)時(shí)刻會(huì)發(fā)生的結(jié)果建模。它建模了一種異步計(jì)算,返回了一個(gè)執(zhí)行預(yù)算結(jié)果的引用。比如,你去干洗店洗衣服,店員會(huì)告訴你什么時(shí)候可以來(lái)取衣服,而不是讓你一直在干洗店等待。要使用Future只需...
摘要:組合式異步編程最近這些年,兩種趨勢(shì)不斷地推動(dòng)我們反思我們?cè)O(shè)計(jì)軟件的方式。第章中介紹的分支合并框架以及并行流是實(shí)現(xiàn)并行處理的寶貴工具它們將一個(gè)操作切分為多個(gè)子操作,在多個(gè)不同的核甚至是機(jī)器上并行地執(zhí)行這些子操作。 CompletableFuture:組合式異步編程 最近這些年,兩種趨勢(shì)不斷地推動(dòng)我們反思我們?cè)O(shè)計(jì)軟件的方式。第一種趨勢(shì)和應(yīng)用運(yùn)行的硬件平臺(tái)相關(guān),第二種趨勢(shì)與應(yīng)用程序的架構(gòu)相關(guān)...
摘要:所以很容易出現(xiàn)某一個(gè)商店的數(shù)據(jù)遲遲無(wú)法返回的情況。工廠方法接受由對(duì)象構(gòu)成的數(shù)組數(shù)組中所有的完成后它返回一個(gè)對(duì)象。異步的可以通過(guò)進(jìn)行合并,無(wú)論他們之間是否有依賴關(guān)系。可以為注冊(cè)一個(gè)回調(diào)函數(shù),在執(zhí)行完畢時(shí)使用。 【最佳價(jià)格查詢器的優(yōu)化 由于我們的兩個(gè)遠(yuǎn)程服務(wù):1.查詢價(jià)格,2.查詢折扣價(jià)格都是基于網(wǎng)絡(luò)的。所以很容易出現(xiàn)某一個(gè)商店的數(shù)據(jù)遲遲無(wú)法返回的情況。由于這些原因,我希望查詢器在查詢時(shí)能...
閱讀 2949·2021-09-28 09:36
閱讀 3823·2021-09-27 13:59
閱讀 2559·2021-08-31 09:44
閱讀 2372·2019-08-30 15:54
閱讀 2408·2019-08-30 15:44
閱讀 1255·2019-08-30 13:45
閱讀 1291·2019-08-29 18:38
閱讀 1313·2019-08-29 18:37