public class CompletableServiceTest { 

    public static void main(String[] args) throws ExecutionException, InterruptedException {

//      test1();

//      test2();

        test3();
} //这种方式提交的任务,有可能任务A是第一个执行完的,但是返回的顺序却不是第一个
public static void test1() throws ExecutionException, InterruptedException {
ExecutorService executorService = Executors.newFixedThreadPool();
List<Callable> task = IntStream.range(, ).boxed().map(CompletableServiceTest::toTask).collect(Collectors.toList());
List<Future<String>> futures = new ArrayList<>();
task.forEach(r -> futures.add(executorService.submit(r))); System.out.println(futures.get().get());
System.out.println("======4======"); System.out.println(futures.get().get());
System.out.println("======3======");
} //这种方式可以保证假如任务A是第一个执行完的,那么他也是第一个返回的
public static void test2() throws ExecutionException, InterruptedException {
ExecutorService executorService = Executors.newFixedThreadPool();
List<Callable> task = IntStream.range(, ).boxed().map(CompletableServiceTest::toTask).collect(Collectors.toList());
CompletionService completionService = new ExecutorCompletionService(executorService);
task.forEach(r -> completionService.submit(r));
Future<?> future = null;
while ((future = completionService.take()) != null){
System.out.println(future.get());
} } //由于调用线程池的shutdownNow方法,可能正在执行的任务被中断后,任务的状态丢失。该任务不包含在shutdownNow的返回值中
//解决的办法是在任务里定义一个状态,表示是否完成
public static void test3() throws ExecutionException, InterruptedException {
ExecutorService executorService = Executors.newFixedThreadPool();
List<Callable> task = IntStream.range(, ).boxed().map(MyTask::new).collect(Collectors.toList());
task.forEach(r -> executorService.submit(r));
TimeUnit.SECONDS.sleep();
executorService.shutdownNow(); //这里虽然返回了未执行完的任务,但是不可性
//由于调用shutdownNow方法,任务被中断没有成功执行完的任务
task.stream().filter(c-> !((MyTask)c).isSuccess()).forEach(c->{System.out.println("task value : " +((MyTask) c).value);}); } public static class MyTask implements Callable<String>{
private final Integer value;
private boolean success = false;
MyTask(int value){
this.value = value;
} @Override
public String call() throws Exception {
System.out.println("task [" + value +"] will be executed");
TimeUnit.SECONDS.sleep(value*+);
System.out.println("task [" + value +"] executes done");
success = true;
return "task result - "+ value;
} public boolean isSuccess(){
return success;
}
} private static Callable<String> toTask(int i){
return ( ) ->{
try {
System.out.println("task [" + i +"] will be executed");
TimeUnit.SECONDS.sleep(i*+);
System.out.println("task [" + i +"] executes done");
return "task result - "+ i;
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}; }
}

CompletableService的更多相关文章

随机推荐

  1. Springboot整合Thmeleaf

    1.概述 Thymeleaf类似JSP.Velocity.Freemarker都是模板引擎,主要用来展示数据,原理如下 springboot官网还是推荐使用Thymeleaf而不是jsp, 不使用js ...

  2. 多模块springboot项目启动访问不了jsp页面

    被springboot项目maven打包.启动.访问折腾的头都大了,一步一个坑,在解决了所有的问题之后,那种欣喜若狂的心情只有自己能体会,决定要好好记录一下了,废话不多说了,直接进入正题. 问题和排查 ...

  3. MongoDB for OPS 02:复制集 RS 配置

    写在前面的话 对于生产环境而言,除非是非常不重要的业务,且该业务允许我们出现一定时间的停机,我们一般才会使用单节点,且该单节点必须要有完善的备份手段. RS 复制集 我们这里采取一主两从的方式搭建复制 ...

  4. c#微信公众号开发一----基本设置,服务器配置token验证,获取timestamp/nonce/signature

    一.c#微信公众号开发----基本设置 参考微信官方文档 https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Acce ...

  5. JS引擎和作用域、编译器之间对话

    关于以下代码段: function foo(a) { console.log( a ); // 2 } foo( 2 ); JS引擎和作用域.编译器之间对话:

  6. HTML中html元素的lang属性的说明

    HTML中html元素的lang属性的说明 我在刚开始学习HTML的时候,关于基本的HTML格式中有一点不明白的地方,基本格式如下 <!DOCTYPE html> <html lan ...

  7. 2019年上半年收集到的人工智能GAN干货文章

    2019年上半年收集到的人工智能GAN干货文章 GAN简介及其常见应用 训练GAN,你应该知道的二三事 了解生成对抗网络(GAN) CosmoGAN:训练GAN,让AI寻找宇宙中的暗物质 关于GAN的 ...

  8. ESP8266与ESP8285开发时有什么区别

    ESP8266模块在WiFi联网领域已经被广泛使用,但是ESP8266芯片是需要外挂Flash芯片的,这样就使模块不能做的更小.之后乐鑫公司又推出了ESP8285芯片,直接集成了1MByte的Flas ...

  9. JEB动态调试解密数据包加密字段

    0x00 场景 在测试某个app的时候,抓取数据包,发现某些参数存在被加密的情况,或者有签名校验的情况,这个时候如果我们想直接去篡改数据包的内容往往是做不到的,那就来看看抓取的某个app登录数据包,如 ...

  10. C语言异常处理

    异常的概念-程序在运行过程中可能产生异常-异常(Exception)与Bug的区别 异常是程序运行时可预料的执行分支 Bug是程序中的错误,是不被预期的运行方式 异常(Exception)和Bug的对 ...