欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

Java并发编程Callable与Future的应用实例代码

时间:2021-09-24 10:58:24|栏目:JAVA代码|点击:

本文主要探究的是java并发编程callable与future的使用,分享了相关实例代码,具体介绍如下。

我们都知道实现多线程有2种方式,一种是继承Thread,一种是实现Runnable,但这2种方式都有一个缺陷,在任务完成后无法获取返回结果。要想获得返回结果,就得使用Callable,Callable任务可以有返回值,但是没法直接从Callable任务里获取返回值;想要获取Callabel任务的返回值,需要用到Future。所以Callable任务和Future模式,通常结合起来使用。

试想一个场景:需要一个帖子列表接口,除了需要返回帖子列表之外,还需要返回每条帖子的点赞列表和评论列表。一页10条帖子来计算,这个接口需要访问21次数据库,访问一次数据库按100ms计算,21次,累计时间为2.1s。这个响应时间,怕是无法令人满意的。怎么办呢?异步化改造接口。

查出帖子列表后,迭代帖子列表,在循环里起10个线程,并发去获取每条帖子的点赞列表,同时另起10个线程,并发去获取每条帖子的评论列表。这样改造之后,接口的响应时间大大缩短,在200ms。这个时候就要用Callabel结合Future来实现。

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(); 
          //查评论列表 
          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); 
          //查点赞列表 
          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); 
        } 
 
      } 
      // 获取所有并发任务的执行结果 
      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 { 
      // 关闭线程池 
      commentPool.shutdown(); 
      supportPool.shutdown(); 
    } 
    return circleResponseList; 
} 

总结

以上就是本文关于Java并发编程Callable与Future的应用实例代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

上一篇:Java内存模型与JVM运行时数据区的区别详解

栏    目:JAVA代码

下一篇:Java下载文件的4种方式总结

本文标题:Java并发编程Callable与Future的应用实例代码

本文地址:http://www.codeinn.net/misctech/177123.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有