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方法的更多相关文章

  1. Linux高级字符设备驱动 poll方法(select多路监控原理与实现)

    1.什么是Poll方法,功能是什么? 2.Select系统调用(功能)      Select系统调用用于多路监控,当没有一个文件满足要求时,select将阻塞调用进程.      int selec ...

  2. Linux 设备驱动--- Poll 方法 --- Select【转】

    转自:http://blog.csdn.net/yikai2009/article/details/8653842 版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[-] Sele ...

  3. Queue接口分析:add和offer区别,remove和poll方法到底啥区别

    Queue接口: public interface Queue<E> extends Collection<E> { /* * add方法,在不违背队列的容量限制的情况,往队列 ...

  4. Python——IO多路复用之select模块poll方法

    Python——IO多路复用之select模块poll方法 使用poll方法实现IO多路复用 .├── poll_client.py├── poll_server.py└── settings.py ...

  5. java高级---->Thread之CompletionService的使用

    CompletionService的功能是以异步的方式一边生产新的任务,一边处理已完成任务的结果,这样可以将执行任务与处理任务分离开来进行处理.今天我们通过实例来学习一下CompletionServi ...

  6. 线程框架Executor的用法举例

    java5线程框架Executor的用法举例 Executor 是 java5 下的一个多任务并发执行框架(Doug Lea),可以建立一个类似数据库连接池的线程池来执行任务.这个框架主要由三个接口和 ...

  7. 线程池:Execution框架

    每问题每线程:在于它没有对已创建线程的数量进行任何限制,除非对客户端能够抛出的请求速率进行限制. 下边 有些图片看不到,清看原地址:http://www.360doc.com/content/10/1 ...

  8. Java并发编程核心方法与框架-CompletionService的使用

    接口CompletionService的功能是以异步的方式一边生产新的任务,一边处理已完成任务的结果,这样可以将执行任务与处理任务分离.使用submit()执行任务,使用take取得已完成的任务,并按 ...

  9. KafkaConsumer 长时间地在poll(long )方法中阻塞

    一,问题描述 搭建的用来测试的单节点Kafka集群(Zookeeper和Kafka Broker都在同一台Ubuntu上),在命令行下使用: ./bin/kafka-topics. --replica ...

随机推荐

  1. 1141 PAT Ranking of Institutions (25 分)

    After each PAT, the PAT Center will announce the ranking of institutions based on their students' pe ...

  2. PHPExcel 读取的几个例子

    1.使用 PHPExcel_IOFactory 读取文件 $objPHPExcel = PHPExcel_IOFactory::load($inputFileName); 2.使用一个特定的读取类,读 ...

  3. PIE SDK Geometry的坐标转换

    1. 基于SpatialReference对象的坐标转换 1.1 示例简介 Geometry类是所有几何形体对象的父类,它是一个抽象类,IGeometry接口定义了所有的几何对象都有的方法和属性. 下 ...

  4. java中的线程(2):如何正确停止线程之3种常见停止方式

    1.常见停止方式 自定义线程,其中含退出标志位,在run中判断它. 使用interrupt()方法中断线程 使用stop方法暴力终止(已经弃用) 2.使用标志位 class TestThread ex ...

  5. C# 清空数组Array.Clear

    using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public ...

  6. HTML盒子尺寸的计算

    参考链接http://edu.51cto.com/lesson/id-54739.html

  7. Thinking in java源码下载链接

    Thinking in java书上显示的下载源码到www.mindview.net站点,但是这个站点打不开了,后来找到真正的下载地址,贴于此. http://www.mindviewinc.com/ ...

  8. 负载均衡服务器中存在大量的TIME_WAIT怎么解决

    首先需要明白什么是TIME_WAIT.TIME_WAIT是在tcp断开连接时进行四次回收的时候,主动断开端在收到被动关闭端的FIN包并发送ACK包给被动关闭后进入的状态.这个状态默认情况下是2倍的MS ...

  9. NSTimer_Block封装定时器的target-action成Block回调

    前言 定时器NSTimer虽然简单易用,但是目标响应机制(target-action)这种方式很容易在代码中出现代码臃肿的情况,特别是在一个文件中有大量的代码,多个定时器的时候不方便调试,因此将NST ...

  10. Whu 1603——Minimum Sum——————【单个元素贡献、滑窗】

    Problem 1603 - Minimum Sum Time Limit: 2000MS   Memory Limit: 65536KB   Total Submit: 623  Accepted: ...