亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java8 使用工廠方法supplyAsync創(chuàng)建CompletableFuture實例

 更新時間:2021年11月04日 15:51:07   作者:小小工匠  
這篇文章主要介紹了Java8 使用工廠方法supplyAsync創(chuàng)建CompletableFuture實例,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

目前為止我們已經(jīng)了解了如何通過編程創(chuàng)建 CompletableFuture 對象以及如何獲取返回值,雖然看起來這些操作已經(jīng)比較方便,但還有進一步提升的空間, CompletableFuture 類自身提供了大量精巧的工廠方法,使用這些方法能更容易地完成整個流程,還不用擔心實現(xiàn)的細節(jié)。

在這里插入圖片描述

可以看到我們使用new Thread的方式,顯然是不恰當?shù)摹?/p>

使用工廠方法 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í)行線程是可行的,后面我們會會介紹如何使用適合你應(yīng)用特性的執(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 對象是完全等價的,這意味著它提供了同樣的錯誤管理機制,而前者你花費了大量的精力才得以構(gòu)建。

在這里插入圖片描述

對CompletableFuture async的理解

驗證代碼如下

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{}{}","結(jié)束");
            return "1";
        },executorService);
       CompletableFuture<String> a2 = CompletableFuture.supplyAsync(() -> {
 
            logger.info("線程2{}{}","開始");
            try {
                TimeUnit.MILLISECONDS.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            logger.info("線程2{}{}","結(jié)束");
            return "1";
        },executorService);
        CompletableFuture<Object> a= a1.thenCombineAsync(a2,(s1,s2) -> {
            logger.info("組合線程{}{}");
            return  s1+s2;
        },executorService);
        Object result = a.get();

當executorService線程池大小為2時候,執(zhí)行結(jié)果如下:

[pool-4-thread-1] INFO test.rcd.thread.CompletableFutureDemo.lambda$mains$4:127 - 組合線程{}{}

a1.thenCombineAsync方法始終被線程1或2執(zhí)行

當executorService線程池大小為3時候,執(zhí)行結(jié)果如下:

[pool-4-thread-3] INFO test.rcd.thread.CompletableFutureDemo.lambda$mains$4:127 - 組合線程{}{}

a1.thenCombineAsync方法始終被線程3執(zhí)行

改為a1.thenCombine(),執(zhí)行結(jié)果:

a1.thenCombineAsync方法始終被線程1或2執(zhí)行

由此可見,async方法始終嘗試取新線程執(zhí)行方法,不帶async方法則會從當前線程里取線程執(zhí)行.CompletableFuture似是與線程無關(guān)的。

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論