重慶分公司,新征程啟航
為企業(yè)提供網(wǎng)站建設(shè)、域名注冊(cè)、服務(wù)器等服務(wù)
為企業(yè)提供網(wǎng)站建設(shè)、域名注冊(cè)、服務(wù)器等服務(wù)
本文主要探究的是java并發(fā)編程callable與future的使用,分享了相關(guān)實(shí)例代碼,具體介紹如下。
十載的瀘溪網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開(kāi)發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。全網(wǎng)整合營(yíng)銷(xiāo)推廣的優(yōu)勢(shì)是能夠根據(jù)用戶(hù)設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整瀘溪建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無(wú)論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)公司從事“瀘溪網(wǎng)站設(shè)計(jì)”,“瀘溪網(wǎng)站推廣”以來(lái),每個(gè)客戶(hù)項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
我們都知道實(shí)現(xiàn)多線程有2種方式,一種是繼承Thread,一種是實(shí)現(xiàn)Runnable,但這2種方式都有一個(gè)缺陷,在任務(wù)完成后無(wú)法獲取返回結(jié)果。要想獲得返回結(jié)果,就得使用Callable,Callable任務(wù)可以有返回值,但是沒(méi)法直接從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ú)法令人滿(mǎn)意的。怎么辦呢?異步化改造接口。
查出帖子列表后,迭代帖子列表,在循環(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 ListcreatePostResponseList(Page page,final String userId){ if(page.getCount()==0||page==null||page.getList()==null){ return null; } //獲取帖子列表 List circleResponseList = page.getList(); int size=circleResponseList.size(); ExecutorService commentPool = Executors.newFixedThreadPool(size); ExecutorService supportPool = Executors.newFixedThreadPool(size); try { List commentFutureList = new ArrayList (size); if (circleResponseList != null && circleResponseList.size() > 0) { for (PostResponse postResponse : circleResponseList) { final String circleId=postResponse.getId(); final String postUserId=postResponse.getUserId(); //查評(píng)論列表 Callable > callableComment = new Callable
>() { @Override public List
call() throws Exception { return circleReviewsBiz.getPostComments(circleId); } }; Future f = commentPool.submit(callableComment); commentFutureList.add(f); //查點(diǎn)贊列表 Callable > callableSupport = new Callable
>() { @Override public List
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 ) f.get(); temp.setSupportList((List ) 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)專(zhuān)題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!