转自: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. Google authenticator 谷歌身份验证,实现动态口令

    Google authenticator 谷歌身份验证,实现动态口令 google authenticator php 服务端 使用PHP类 require_once '../PHPGangsta/G ...

  2. NancyFx框架之检测任务管理器

    先建一个空的项目和之前的NancyFx系列一样的步骤 然后建三个文件夹Models,Module,Views 然后分别安装一下组件 jQuery Microsoft.AspNet.SignalR Mi ...

  3. node开发一个接口详细步骤

    最近在做后台系统改版,由于目前接口还没出来,就自己用nodejs写了个简单的接口. 我这里用的是nodejs+mysql的 这里不讲nodejs和mysql的安装.这些基础略过. 首先创建文件夹.cd ...

  4. 详聊js中的原型/原型链

    先以一段简单的代码为例: function Dog(params){     this.name = param.name;     this.age = param.age;     this.ba ...

  5. python 简易小爬虫

    此脚本用于爬站点的下载链接,最终输出到txt文档中. 如果是没有防盗链设置的站点,也可以使用脚本中的下载函数尝试直接下载. 本脚本是为了短期特定目标设计的,如果使用它爬其它特征的资源链接需自行修改配置 ...

  6. Linux系统启动过程浅析

    经过老师的讲解以及查阅资料后,现对Linux系统启动做以浅析,仅是个人理解. 主要的步骤有以下几步: 第一步:Power On.用户按下电源开关的那一瞬间,叫Power On阶段 .在这个阶段,BIO ...

  7. VS 2019编辑含有资源文件.resx的项目时提示MSB3086 任务未能使用 SdkToolsPath 或注册表项找到“al.exe”

    环境: Win10 X64, VS2019 错误提示: 错误 MSB3086 任务未能使用 SdkToolsPath“”或注册表项“HKEY_LOCAL_MACHINE\SOFTWARE\Micros ...

  8. linux使用v 2ray

    一.安装配置服务端程序 是时候使用 了,因为相对安全,使用方法很简单,使用root权限执行以下命令即可 $ sudo -i 一顿安装后如图 输入 命令可以查看链接,然后在客户端使用这个链接就能配置好了 ...

  9. java中的Excel导出功能

    public void exportExcel(Long activityId, HttpServletResponse response) throws IOException { // 获取统计报 ...

  10. 在 CentOS 上部署 GitLab (自托管的Git项目仓库)

    参考资料https://github.com/mattias-ohlsson/gitlab-installer/blob/master/gitlab-install-el6.sh 环境准备OS: Ce ...