Java并發(fā)編程Callable與Future的應(yīng)用實(shí)例代碼
本文主要探究的是java并發(fā)編程callable與future的使用,分享了相關(guān)實(shí)例代碼,具體介紹如下。
我們都知道實(shí)現(xiàn)多線程有2種方式,一種是繼承Thread,一種是實(shí)現(xiàn)Runnable,但這2種方式都有一個(gè)缺陷,在任務(wù)完成后無(wú)法獲取返回結(jié)果。要想獲得返回結(jié)果,就得使用Callable,Callable任務(wù)可以有返回值,但是沒法直接從Callable任務(wù)里獲取返回值;想要獲取Callabel任務(wù)的返回值,需要用到Future。所以Callable任務(wù)和Future模式,通常結(jié)合起來(lái)使用。
試想一個(gè)場(chǎng)景:需要一個(gè)帖子列表接口,除了需要返回帖子列表之外,還需要返回每條帖子的點(diǎn)贊列表和評(píng)論列表。一頁(yè)10條帖子來(lái)計(jì)算,這個(gè)接口需要訪問(wèn)21次數(shù)據(jù)庫(kù),訪問(wèn)一次數(shù)據(jù)庫(kù)按100ms計(jì)算,21次,累計(jì)時(shí)間為2.1s。這個(gè)響應(yīng)時(shí)間,怕是無(wú)法令人滿意的。怎么辦呢?異步化改造接口。
查出帖子列表后,迭代帖子列表,在循環(huán)里起10個(gè)線程,并發(fā)去獲取每條帖子的點(diǎn)贊列表,同時(shí)另起10個(gè)線程,并發(fā)去獲取每條帖子的評(píng)論列表。這樣改造之后,接口的響應(yīng)時(shí)間大大縮短,在200ms。這個(gè)時(shí)候就要用Callabel結(jié)合Future來(lái)實(shí)現(xiàn)。
private List<PostResponse> createPostResponseList(Page<PostResponse> page,final String userId){
if(page.getCount()==0||page==null||page.getList()==null){
return null;
}
//獲取帖子列表
List<PostResponse> circleResponseList = page.getList();
int size=circleResponseList.size();
ExecutorService commentPool = Executors.newFixedThreadPool(size);
ExecutorService supportPool = Executors.newFixedThreadPool(size);
try {
List<Future> commentFutureList = new ArrayList<Future>(size);
if (circleResponseList != null && circleResponseList.size() > 0) {
for (PostResponse postResponse : circleResponseList) {
final String circleId=postResponse.getId();
final String postUserId=postResponse.getUserId();
//查評(píng)論列表
Callable<List<CircleReviews>> callableComment = new Callable<List<CircleReviews>>() {
@Override
public List<CircleReviews> call() throws Exception {
return circleReviewsBiz.getPostComments(circleId);
}
};
Future f = commentPool.submit(callableComment);
commentFutureList.add(f);
//查點(diǎn)贊列表
Callable<List<CircleZan>> callableSupport = new Callable<List<CircleZan>>() {
@Override
public List<CircleZan> call() throws Exception {
return circleZanBiz.findList(circleId);
}
};
Future supportFuture = supportPool.submit(callableSupport);
commentFutureList.add(supportFuture);
}
}
// 獲取所有并發(fā)任務(wù)的執(zhí)行結(jié)果
int i = 0;
PostResponse temp = null;
for (Future f : commentFutureList) {
temp = circleResponseList.get(i);
temp.setCommentList((List<CircleReviews>) f.get();
temp.setSupportList((List<CircleZan>) f.get();
circleResponseList.set(i, temp);
i++;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 關(guān)閉線程池
commentPool.shutdown();
supportPool.shutdown();
}
return circleResponseList;
}
總結(jié)
以上就是本文關(guān)于Java并發(fā)編程Callable與Future的應(yīng)用實(shí)例代碼的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!
相關(guān)文章
Java8新特性之精簡(jiǎn)的JRE詳解_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了Java8新特性之精簡(jiǎn)的JRE詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06
java實(shí)現(xiàn)清理DNS Cache的方法
這篇文章主要介紹了java實(shí)現(xiàn)清理DNS Cache的方法,分析了幾種常用的清理方法,并給出了反射清理的完整實(shí)例,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-01-01
舉例講解Java的Spring框架中AOP程序設(shè)計(jì)方式的使用
這篇文章主要介紹了Java的Spring框架中AOP程序設(shè)計(jì)方式的使用講解,文中舉的AOP下拋出異常的例子非常實(shí)用,需要的朋友可以參考下2016-04-04
Java中ThreadLocal線程變量的實(shí)現(xiàn)原理
本文主要介紹了Java中ThreadLocal線程變量的實(shí)現(xiàn)原理,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06

