Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 268|回复: 0

[默认分类] 深入学习理解java:CompletionService解决ExecutorService的submit方法的缺点

[复制链接]
  • TA的每日心情
    开心
    2021-12-13 21:45
  • 签到天数: 15 天

    [LV.4]偶尔看看III

    发表于 2018-5-15 10:23:21 | 显示全部楼层 |阅读模式
            在ExecutorService的submit方法中可以获取返回值,通过Future的get方法,但是这个Future类存在缺陷,Future接口调用get()方法取得处理后的返回结果时具有阻塞性,也就是说调用Future的get方法时,任务没有执行完成,则get方法要一直阻塞等到任务完成为止。
    这样大大的影响了系统的性能,这就是Future的最大缺点。为此,java1.5以后提供了CompletionServlice来解决这个问题。
    CompletionService
    接口CompletionService的功能是异步的方式,一边生产任务,一边处理完成的任务结果,这样可以将执行的任务与处理任务隔离开来进行处理,使用submit执行任务,使用塔克获取已完成的任务,并按照这些任务的完成的时间顺序来处理他们的结果。
         如图这个是CompletionService的核心方法
         
    其有构造器如下;
    1. [code]        CompletionService<T> completionService=new ExecutorCompletionService<T>(executorService);
    复制代码
    [/code]可以发现,需要传入一个executor,
    下面demo来解释CompletionService解决Future的缺点。
    首先创建一个类
    1. [code]package com.completion;
    2. import java.util.concurrent.Callable;
    3. public class MyCallable implements Callable<String>{
    4.     private String name;
    5.     private long sleep;
    6.     public MyCallable(String name,long sleep) {
    7.         super();
    8.         this.name=name;
    9.         this.sleep=sleep;
    10.     }
    11.     @Override
    12.     public String call() throws Exception {
    13.     Thread.sleep(sleep);
    14.         return "call()---->"+name;
    15.     }
    16. }
    复制代码
    [/code]测试方法
    1. [code]package com.completion;
    2. import java.util.ArrayList;
    3. import java.util.List;
    4. import java.util.concurrent.Callable;
    5. import java.util.concurrent.CompletionService;
    6. import java.util.concurrent.ExecutionException;
    7. import java.util.concurrent.Executor;
    8. import java.util.concurrent.ExecutorCompletionService;
    9. import java.util.concurrent.ExecutorService;
    10. import java.util.concurrent.Executors;
    11. import javax.annotation.processing.Completion;
    12. public class Test {
    13.     public static void main(String[] args) throws InterruptedException, ExecutionException {
    14.            //模拟不同的耗时
    15.         MyCallable callable=new MyCallable("小double", 1000);
    16.         MyCallable callable1=new MyCallable("大double", 4000);
    17.         MyCallable callable2=new MyCallable("中double", 3000);
    18.         List<Callable<String>>list=new ArrayList<>();
    19.         list.add(callable);
    20.         list.add(callable1);
    21.         list.add(callable2);
    22.         ExecutorService executorService=Executors.newCachedThreadPool();
    23.         CompletionService<String>completionService=new ExecutorCompletionService<>(executorService);
    24.         for (Callable<String>ca:list) {
    25.         completionService.submit(ca);   
    26.         }
    27.         for (Callable<String>ca:list) {
    28.             System.out.println("-------------");
    29.             System.err.println(completionService.take().get());
    30.             }
    31.     }
    32. }
    复制代码
    [/code]运行效果
      
    如此一来,,,,哈哈!Android的mvp你懂得!!!!                    $(function () {                $("pre.prettyprint code").each(function () {                    var lines = $(this).text().split("\n").length;                    var $numbering = $("").addClass("pre-numbering").hide();                    $(this).addClass("has-numbering").parent().append($numbering);                    for (i = 1; i ").text(i));                    };                    $numbering.fadeIn(1700);                });            });           
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-3-29 09:24 , Processed in 0.374861 second(s), 46 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表