转自:https://blog.csdn.net/jdsjlzx/article/details/52912701

FutureTask既是Future、Runnable,又是包装了Callable(如果是Runnable最终也会被转换为Callable ), 它是这两者的合体。

package io.renren.test2;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*; public class test2 { public static void main(String[] args) throws ExecutionException, InterruptedException {
testFuture();
// testCompletionService();
} //结果的输出和线程的放入顺序 有关(如果前面的没完成,就算后面的哪个完成了也得等到你的牌号才能输出!),so阻塞耗时
public static void testFuture() throws InterruptedException, ExecutionException {
System.out.println("--1.1--> main Thread begin:");
ExecutorService executor = Executors.newCachedThreadPool();
List<Future<String>> result = new ArrayList<Future<String>>();
for (int i = 0; i < 10; i++) {
Future<String> submit = executor.submit(new Task(i));
result.add(submit);
}
executor.shutdown();
for (int i = 0; i < 10; i++) {//一个一个等待返回结果
System.out.println("--1.2--> 一个一个等待返回结果: " + result.get(i).get());
}
System.out.println("--1.3--> main Thread end:");
} //结果的输出和线程的放入顺序 无关(谁完成了谁就先输出!主线程总是能够拿到最先完成的任务的返回值,而不管它们加入线程池的顺序),so很大大缩短等待时间
private static void testCompletionService() throws InterruptedException, ExecutionException {
System.out.println("--2.2--> main Thread begin:");
ExecutorService executor = Executors.newCachedThreadPool();
ExecutorCompletionService<String> completionService = new ExecutorCompletionService<>(executor);
for (int i = 0; i < 10; i++) {
completionService.submit(new Task(i));
}
executor.shutdown();
for (int i = 0; i < 10; i++) {
// 检索并移除表示下一个已完成任务的 Future,如果目前不存在这样的任务,则等待。
Future<String> future = completionService.take(); //这一行没有完成的任务就阻塞
System.out.println("--2.3--> " + future.get()); // 这一行在这里不会阻塞,引入放入队列中的都是已经完成的任务
}
System.out.println("--2.4--> main Thread end:");
} private static class Task implements Callable<String> { private volatile int i; public Task(int i) {
this.i = i;
} @Override
public String call() throws Exception {
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName());
return "--0.1--> 任务 : " + i;
} }
}
testFuture() 打印:
--1.1--> main Thread begin:
pool-1-thread-9
pool-1-thread-2
pool-1-thread-1
pool-1-thread-3
pool-1-thread-6
pool-1-thread-7
pool-1-thread-5
pool-1-thread-8
pool-1-thread-4
--1.2--> 一个一个等待返回结果: --0.1--> 任务 : 0
--1.2--> 一个一个等待返回结果: --0.1--> 任务 : 1
--1.2--> 一个一个等待返回结果: --0.1--> 任务 : 2
--1.2--> 一个一个等待返回结果: --0.1--> 任务 : 3
--1.2--> 一个一个等待返回结果: --0.1--> 任务 : 4
--1.2--> 一个一个等待返回结果: --0.1--> 任务 : 5
--1.2--> 一个一个等待返回结果: --0.1--> 任务 : 6
--1.2--> 一个一个等待返回结果: --0.1--> 任务 : 7
--1.2--> 一个一个等待返回结果: --0.1--> 任务 : 8
pool-1-thread-10
--1.2--> 一个一个等待返回结果: --0.1--> 任务 : 9
--1.3--> main Thread end:
testCompletionService() 打印:
--2.2--> main Thread begin:
pool-1-thread-1
--2.3--> --0.1--> 任务 : 0
pool-1-thread-2
pool-1-thread-7
--2.3--> --0.1--> 任务 : 1
--2.3--> --0.1--> 任务 : 6
pool-1-thread-3
--2.3--> --0.1--> 任务 : 2
pool-1-thread-8
--2.3--> --0.1--> 任务 : 7
pool-1-thread-9
--2.3--> --0.1--> 任务 : 8
pool-1-thread-6
pool-1-thread-5
pool-1-thread-4
--2.3--> --0.1--> 任务 : 5
--2.3--> --0.1--> 任务 : 4
--2.3--> --0.1--> 任务 : 3
pool-1-thread-10
--2.3--> --0.1--> 任务 : 9
--2.4--> main Thread end:

CompletionService的好处与使用场景的更多相关文章

  1. Java批处理ExecutorService/CompletionService

    服务端接收一个请求,常常需要同时进行几个计算或者向其他服务发送请求,最后拼装结果返回上游.本文就来看下JDK提供几个并行处理方案,牵涉到ExcecutorService/CompletionServi ...

  2. [ZZ] Maxwell 架构

    http://digi.163.com/14/0218/23/9LDCTFON00162DSP.html [IT168 评测]随着一句“娘娘,封神啦(宝鸡口音)”,中国的观众迅速认识到了两个极其出彩的 ...

  3. Activiti工作流学习-----基于5.19.0版本(5)

    五.与Spring集成 实际项目中一般都有Spring的身影,与Spring集成使得Activiti的实用性得到提高.activiti和Spring整合需要activiti-spring的jar在类路 ...

  4. 图解Javascript——作用域、作用域链、闭包

    什么是作用域? 作用域是一种规则,在代码编译阶段就确定了,规定了变量与函数的可被访问的范围.全局变量拥有全局作用域,局部变量则拥有局部作用域. js是一种没有块级作用域的语言(包括if.for等语句的 ...

  5. jquery源码 Callback

    工具方法.对函数的统一管理. jquery2.0.3版本$.Callback()部分的源码如下: // String to Object options format cache var option ...

  6. js 作用域,作用域链,闭包

    什么是作用域? 作用域是一种规则,在代码编译阶段就确定了,规定了变量与函数的可被访问的范围.全局变量拥有全局作用域,局部变量则拥有局部作用域. js是一种没有块级作用域的语言(包括if.for等语句的 ...

  7. Docke--Dockerfile指令介绍

    Dockerfile 构建镜像常用指令 Dockerfile 是一个文本文件,其内包含了一条条的指定(Instruction),每一条指令构建一层,因此每一条指定的内容,就是描述该层应当如何构建. 通 ...

  8. pyqtgraph

    安装  pip3 install pyqtgraph 在PyQtGraph中,有几种绘制图形的方法: pyqtgraph.plot():创建一个新的绘图窗口来显示数据: PlotWidget.plot ...

  9. java异步编程降低延迟

    目录 java异步编程降低延迟 一.ExecutorService和CompletionService 二.CompletableFuture(重要) 三.stream中的parallel(并行流) ...

随机推荐

  1. vue主动刷新页面及列表数据删除后的刷新方法

    在继刷新当前页面,重载页面数据那篇之后 那一篇 深入理解数据驱动 以上算是开发过程中的一个坑,用了一段时间,今天再读代码的时候,感觉被坑的很严重. 1. 获取列表方法 2.重新获取数据 3.这样再次调 ...

  2. ffmpeg知多少~~~

    一.ffmpeg安装: https://jingyan.baidu.com/article/f7ff0bfcd64cea2e26bb1334.html   二.ffmpeg视频处理(包括各种视频流处理 ...

  3. Kata Container 介绍

    docker容器,性能高,不安全:VM虚拟机,安全性好,性能损耗大:Kata Container轻量级虚拟机的容器,即安全,性能也高. 开源容器项目Kata Containers,旨在将虚拟机(VM) ...

  4. UNIX网络编程总结四

    socket: 为了执行网络I/O,一个进程做的第一件事就是调用socket函数. family指明协议族,type指明类型,除非在原始套接口,protocol一般为0,并非所有的family,typ ...

  5. OGG-00303

    解决方案:因为Defgen版本不同,注释数据定义文件对应的行 *Database type:ORACLE,用*在行首注释即可

  6. sysbench github & manual

    sysbench github https://github.com/akopytov/sysbench sysbench-manual.pdf https://github.com/mrivandu ...

  7. Linux命令行工具之vmstat命令

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11484608.html vmstat是一款指定采样周期和次数的功能性监测工具,可以使用它监控进程上下文 ...

  8. ckeditor如何能实现直接粘贴把图片上传到服务器中?

    在之前在工作中遇到在富文本编辑器中粘贴图片不能展示的问题,于是各种网上扒拉,终于找到解决方案,在这里感谢一下知乎中众大神以及TheViper. 通过知乎提供的思路找到粘贴的原理,通过TheViper找 ...

  9. 【CF1257D】Yet Another Monster Killing Problem【贪心】

    题意:给定一些怪物,每天可以选一个勇士进去打怪,每个勇士每天只能打不超过si个怪物,每个勇士只能打能力值≤pi的怪物,问最少多少天打完所有怪物 题解:贪心,每天尽可能多的去打怪,那么存一个对于长度为i ...

  10. linux centos添加yum镜像

    下载配置文件,加入/etc/yum.repos.d/阿里镜像:http://mirrors.aliyun.com/repo/Centos-7.repo网易镜像:http://mirrors.163.c ...