詳解Java CompletableFuture使用方法以及與FutureTask的區(qū)別
總的來(lái)說(shuō)簡(jiǎn)潔了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); // 不用手動(dòng)提交了
System.out.println(future1.get());
還有很多異步回調(diào), 組合處理
創(chuàng)建任務(wù)
1. .supplyAsync
創(chuàng)建一個(gè)帶返回值的任務(wù)
2. .runAsync
創(chuàng)建一個(gè)不帶返回值的任務(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è)方法都有兩個(gè)構(gòu)造方法, 默認(rèn)不指定自定義線程池, 他會(huì)指定默認(rèn)的提交任務(wù)的方法
// 查看cpu的核數(shù)是否大于1核
private static final boolean useCommonPool =
(ForkJoinPool.getCommonPoolParallelism() > 1);
// 如果大于1核 則調(diào)用execute方法, 每次創(chuàng)建一個(gè)線程
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)異常時(shí), 會(huì)終止任務(wù),get的時(shí)候會(huì)拋出主任務(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());

去掉異常

組合處理
....
就是將多個(gè)任務(wù)組合起來(lái)執(zhí)行, 時(shí)間原因, 這里我就不介紹了, 大家另行百度吧
到此這篇關(guān)于詳解Java CompletableFuture使用方法的文章就介紹到這了,更多相關(guān)Java CompletableFuture內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
idea啟動(dòng)springmvc項(xiàng)目時(shí)報(bào)找不到類的解決方法
這篇文章主要介紹了idea啟動(dòng)springmvc項(xiàng)目時(shí)報(bào)找不到類的解決方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
SpringBoot Mybatis動(dòng)態(tài)數(shù)據(jù)源切換方案實(shí)現(xiàn)過(guò)程
這篇文章主要介紹了SpringBoot+Mybatis實(shí)現(xiàn)動(dòng)態(tài)數(shù)據(jù)源切換方案過(guò)程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
詳解Java的內(nèi)置異常以及創(chuàng)建自定義異常子類的方法
這篇文章主要介紹了詳解Java的內(nèi)置異常以及創(chuàng)建自定義異常子類的方法,是Java入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-09-09
Java經(jīng)典排序算法之冒泡排序代碼實(shí)例
這篇文章主要介紹了Java經(jīng)典排序算法之冒泡排序代碼實(shí)例,相鄰兩元素進(jìn)行比較,如過(guò)左側(cè)元素大于右側(cè)元素,則進(jìn)行交換,每完成一次循環(huán)就將最大元素排在最后,下一次循環(huán)是將其它的數(shù)進(jìn)行類似操作,需要的朋友可以參考下2023-11-11
Mybatis使用@Select注解sql中使用in問(wèn)題
這篇文章主要介紹了Mybatis使用@Select注解sql中使用in問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05
MyBatis實(shí)現(xiàn)多表聯(lián)合查詢r(jià)esultType的返回值
這篇文章主要介紹了MyBatis多表聯(lián)合查詢r(jià)esultType的返回值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
Java如何高效實(shí)現(xiàn)Word文檔對(duì)比
在項(xiàng)目協(xié)作、文檔審核或版本迭代的快節(jié)奏工作中,你是否曾為Word文檔的細(xì)微修改而抓狂,下面我們就來(lái)一起探討如何利用Java進(jìn)行Word文檔的自動(dòng)化比較吧2025-11-11
Java?CyclicBarrier錯(cuò)誤使用的實(shí)例
文章描述了一個(gè)Java程序使用CyclicBarrier實(shí)現(xiàn)兩個(gè)線程交替打印字母和數(shù)字時(shí)遇到的問(wèn)題,由于線程執(zhí)行順序不當(dāng),導(dǎo)致輸出結(jié)果與預(yù)期不符,通過(guò)調(diào)整線程的等待和打印順序,解決了問(wèn)題,文章還提供了一個(gè)控制流圖來(lái)詳細(xì)解釋修改前后的程序邏輯2025-01-01

