转自: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-fiters过滤器的使用

    1.定义过滤器 2.使用过滤器 ...... <el-table-column prop="user_gender" align="center" lab ...

  2. Oracle 索引数据字典、基于函数的索引

    user_indexes 字典视图包含了索引名和唯一性, user_ind_columns视图包含了索引名.表名.以及列名 dba_indexes dba_ind_columns 同理 select ...

  3. oracle 使用escape转义%与_匹配字符为本来含义

    举例: 查找姓名为M%的员工. select * from employee where staff_name like 'M\%' escape '\';

  4. JS原型链详解(2)

    深入理解javascript原型链 在javascript中原型和原型链是一个很神奇的东西,对于大多数人也是最难理解的一部分,掌握原型和原型链的本质是javascript进阶的重要一环.今天我分享一下 ...

  5. mysql服务设置远程连接

    一.前期准备 1.虚拟机/物理机    mysql环境(非本机)2.本机 navicat软件(验证远程连接) 二 .mysql配置 1.在远程主机的本机   使用root用户连接mysql mysql ...

  6. 【leetcode】1091. Shortest Path in Binary Matrix

    题目如下: In an N by N square grid, each cell is either empty (0) or blocked (1). A clear path from top- ...

  7. [原创] Delphi Win API函数 操作帮助文件 HtmlHelpA函数介绍

    Delphi Win API函数 操作帮助文件 HtmlHelpA函数介绍 函数原型:HWND HtmlHelpA( HWND hwndCaller, LPCSTR pszFile, UINT uCo ...

  8. Linux学习-MySQL的主从复制高可用性解决方案MHA

    一.MHA简介 1.MHA Master High Availability,对主节点进行监控,可实现自动故障转移至其它从节点:通过提升某一从节点为新的主节点,基于主从复制实现,还需要客户端配合实现, ...

  9. 如何将数组中的元组包转化为字典通过json序列化给前端

  10. RTF筛选工具

    您可借助此项筛选程序对具备.rtf扩展名的文档进行爬行遍历,以便通过搜索结果对目标文档加以访问调用.管理员应先在相关服务器上对程序文件进行注册,方可运行此项工具. 用户下载.安装并使用相关代码的行为即 ...