欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

在這里插入圖片描述

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

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

采用 supplyAsync 方法后,可以用一行代碼重寫getPriceAsync 方法。

【使用工廠方法 supplyAsync 創(chuàng)建 CompletableFuture 對(duì)象】

public Future<Double> getPriceAsync(String product) {
	return CompletableFuture.supplyAsync(() -> calculatePrice(product));
}

supplyAsync 方法接受一個(gè)生產(chǎn)者( Supplier )作為參數(shù),返回一個(gè) CompletableFuture對(duì)象,該對(duì)象完成異步執(zhí)行后會(huì)讀取調(diào)用生產(chǎn)者方法的返回值。

生產(chǎn)者方法會(huì)交由 ForkJoinPool池中的某個(gè)執(zhí)行線程( Executor )運(yùn)行,但是你也可以使用 supplyAsync 方法的重載版本,傳遞第二個(gè)參數(shù)指定不同的執(zhí)行線程執(zhí)行生產(chǎn)者方法。

一般而言,向 CompletableFuture 的工廠方法傳遞可選參數(shù),指定生產(chǎn)者方法的執(zhí)行線程是可行的,后面我們會(huì)會(huì)介紹如何使用適合你應(yīng)用特性的執(zhí)行線程改善程序的性能。

在這里插入圖片描述

對(duì)比

剛剛的代碼

public Future<Double> getPriceAsync(String product) {
	return CompletableFuture.supplyAsync(() -> calculatePrice(product));
}

getPriceAsync 方法返回的 CompletableFuture 對(duì)象和 下面的代碼

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 對(duì)象是完全等價(jià)的,這意味著它提供了同樣的錯(cuò)誤管理機(jī)制,而前者你花費(fèi)了大量的精力才得以構(gòu)建。

在這里插入圖片描述

對(duì)CompletableFuture async的理解

驗(yàn)證代碼如下

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();

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

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

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

當(dāng)executorService線程池大小為3時(shí)候,執(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方法則會(huì)從當(dāng)前線程里取線程執(zhí)行.CompletableFuture似是與線程無關(guān)的。

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

相關(guān)文章

最新評(píng)論