CompletionService的poll方法
1、poll():马上返回完成的任务,若没有,则返回null
2、poll(long timeout, TimeUnit unit): 等待timeout时间,如果大于最短任务完成时间,则获取任务结果返回,结束等待;如果小于任务完成时间,则等待任务完成,获取结果并返回
实验代码:
import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit; public class MyPoll { public static void main(String[] args) {
// TODO 自动生成的方法存根
ExecutorService executor=Executors.newCachedThreadPool();
CompletionService<String> comservice=new ExecutorCompletionService<String>(executor);
MyPollCallble callable=new MyPollCallble();
MyPollCallble_time time_cable=new MyPollCallble_time();
comservice.submit(callable);
comservice.submit(time_cable);
//poll():获取并移除表示下一个已完成任务的future。如果不存完成的任务,则返回null,即不会出现阻塞效果
// System.out.println(comservice.poll());
try {
//等待指定timeout时间,在timeout之内获得值即向下执行,如果超时也向下执行
System.out.println("get1 at"+System.currentTimeMillis());
System.out.println("1 "+comservice.poll(1000, TimeUnit.SECONDS).get());
System.out.println("get2 at"+System.currentTimeMillis());
System.out.println("2 "+comservice.poll(2000, TimeUnit.SECONDS).get());
System.out.println("get3 at"+System.currentTimeMillis());
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (ExecutionException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
} }
class MyPollCallble implements Callable<String>{ @Override
public String call() throws Exception {
// TODO 自动生成的方法存根
System.out.println("start");
System.out.println("first "+System.currentTimeMillis());
Thread.sleep(2000);
System.out.println("end "+System.currentTimeMillis());
return "finish";
} }
class MyPollCallble_time implements Callable<String>{ @Override
public String call() throws Exception {
// TODO 自动生成的方法存根
System.out.println("start time");
System.out.println("second "+System.currentTimeMillis());
Thread.sleep(3000);
System.out.println("end time "+System.currentTimeMillis());
return "finish time";
} }
实验结果:
start
first 1492676279057
get1 at1492676279057
start time
second 1492676279057
end 1492676281061
1 finish
get2 at1492676281061
end time 1492676282064
2 finish time
get3 at1492676282064
可以看到get1与get2之间相差2秒,表明是在等待任务完成,而不是未获得值继续向下执行
CompletionService的poll方法的更多相关文章
- Linux高级字符设备驱动 poll方法(select多路监控原理与实现)
1.什么是Poll方法,功能是什么? 2.Select系统调用(功能) Select系统调用用于多路监控,当没有一个文件满足要求时,select将阻塞调用进程. int selec ...
- Linux 设备驱动--- Poll 方法 --- Select【转】
转自:http://blog.csdn.net/yikai2009/article/details/8653842 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[-] Sele ...
- Queue接口分析:add和offer区别,remove和poll方法到底啥区别
Queue接口: public interface Queue<E> extends Collection<E> { /* * add方法,在不违背队列的容量限制的情况,往队列 ...
- Python——IO多路复用之select模块poll方法
Python——IO多路复用之select模块poll方法 使用poll方法实现IO多路复用 .├── poll_client.py├── poll_server.py└── settings.py ...
- java高级---->Thread之CompletionService的使用
CompletionService的功能是以异步的方式一边生产新的任务,一边处理已完成任务的结果,这样可以将执行任务与处理任务分离开来进行处理.今天我们通过实例来学习一下CompletionServi ...
- 线程框架Executor的用法举例
java5线程框架Executor的用法举例 Executor 是 java5 下的一个多任务并发执行框架(Doug Lea),可以建立一个类似数据库连接池的线程池来执行任务.这个框架主要由三个接口和 ...
- 线程池:Execution框架
每问题每线程:在于它没有对已创建线程的数量进行任何限制,除非对客户端能够抛出的请求速率进行限制. 下边 有些图片看不到,清看原地址:http://www.360doc.com/content/10/1 ...
- Java并发编程核心方法与框架-CompletionService的使用
接口CompletionService的功能是以异步的方式一边生产新的任务,一边处理已完成任务的结果,这样可以将执行任务与处理任务分离.使用submit()执行任务,使用take取得已完成的任务,并按 ...
- KafkaConsumer 长时间地在poll(long )方法中阻塞
一,问题描述 搭建的用来测试的单节点Kafka集群(Zookeeper和Kafka Broker都在同一台Ubuntu上),在命令行下使用: ./bin/kafka-topics. --replica ...
随机推荐
- LOJ #6432. 「PKUSC2018」真实排名
题目在这里...... 对于这道题,现场我写炸了......谁跟我说组合数O(n)的求是最快的?(~!@#¥¥%……& #include <cstdio> #include < ...
- Android配置横屏资源与Activity生命周期
屏幕旋转会改变设备配置(device configguration).设备设置的特征有:屏幕方向.屏幕像素密度.屏幕尺寸.键盘类型.底座模式以及语言等. 当屏幕发现旋转时(设备配置更改),And ...
- js的apply和call
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Farey Sequence(欧拉函数板子题)
题目链接:http://poj.org/problem?id=2478 Farey Sequence Time Limit: 1000MS Memory Limit: 65536K Total S ...
- (转)模块readline解析
模块readline解析 原文:https://www.cnblogs.com/fireflow/p/4841413.html readline模块定义了一系列函数用来读写Python解释器中历史命令 ...
- lua "诡异"的return用法
https://yq.aliyun.com/articles/11387 lua "诡异"的return用法 德哥 2016-03-29 15:38:42 浏览5690 评论0 ...
- React.js 小书 Lesson14 - 实战分析:评论功能(一)
作者:胡子大哈 原文链接:http://huziketang.com/books/react/lesson14 转载请注明出处,保留原文链接和作者信息. 课程到这里大家已经掌握了 React.js 的 ...
- oracle dblink简介
database link概述 database link是定义一个数据库到另一个数据库的路径的对象,database link允许你查询远程表及执行远程程序.在任何分布式环境里,database都是 ...
- FontAwesome 图标 class="fa fa-home"
本文转自:http://www.yeahzan.com/fa/facss.html 引用方式: class="fa fa-home" 注意:每个图标的引用都必须要添加 fa fa- ...
- [转]JS组件系列——BootstrapTable 行内编辑解决方案:x-editable
本文转自:http://www.cnblogs.com/landeanfen/p/5821192.html 阅读目录 一.x-editable组件介绍 二.bootstrapTable行内编辑初始方案 ...