附加:Java 4种线程池介绍请查看

谈谈new Thread的弊端及Java四种线程池的使用

当我们通过Executor提交一组并发执行的任务,并且希望在每一个任务完成后能立即得到结果,有两种方式可以采取:

方式一:

通过一个list来保存一组future,然后在循环中轮训这组future,直到每个future都已完成。如果我们不希望出现因为排在前面的任务阻塞导致后面先完成的任务的结果没有及时获取的情况,那么在调用get方式时,需要将超时时间设置为0

public class CompletionServiceTest {  

    static class Task implements Callable<String>{
private int i; public Task(int i){
this.i = i;
} @Override
public String call() throws Exception {
Thread.sleep(10000);
return Thread.currentThread().getName() + "执行完任务:" + i;
}
} public static void main(String[] args){
testUseFuture();
} private static void testUseFuture(){
int numThread = 5;
ExecutorService executor = Executors.newFixedThreadPool(numThread);
List<Future<String>> futureList = new ArrayList<Future<String>>();
for(int i = 0;i<numThread;i++ ){
Future<String> future = executor.submit(new CompletionServiceTest.Task(i));
futureList.add(future);
} while(numThread > 0){
for(Future<String> future : futureList){
String result = null;
try {
result = future.get(0, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (TimeoutException e) {
//超时异常直接忽略
            //future.cancel(true);//超时设置任务取消
}
if(null != result){
futureList.remove(future);
numThread--;
System.out.println(result);
//此处必须break,否则会抛出并发修改异常。(也可以通过将futureList声明为CopyOnWriteArrayList类型解决)
break;
}
}
}
}
}

方式二:

第一种方式显得比较繁琐,通过使用ExecutorCompletionService,则可以达到代码最简化的效果。

public class CompletionServiceTest {  

    static class Task implements Callable<String>{
private int i; public Task(int i){
this.i = i;
} @Override
public String call() throws Exception {
Thread.sleep(10000);
return Thread.currentThread().getName() + "执行完任务:" + i;
}
} public static void main(String[] args) throws InterruptedException, ExecutionException{
testExecutorCompletionService();
} private static void testExecutorCompletionService() throws InterruptedException, ExecutionException{
int numThread = 3;
ExecutorService executor = Executors.newFixedThreadPool(numThread);
CompletionService<String> completionService = new ExecutorCompletionService<String>(executor);
for(int i = 0;i<numThread;i++ ){
completionService.submit(new CompletionServiceTest.Task(i));
}
} for(int i = 0;i<numThread;i++ ){
System.out.println(completionService.take().get()); //获取执行结果
} }

ExecutorCompletionService分析:

CompletionService是Executor和BlockingQueue的结合体。

    public ExecutorCompletionService(Executor executor) {
if (executor == null)
throw new NullPointerException();
this.executor = executor;
this.aes = (executor instanceof AbstractExecutorService) ?
(AbstractExecutorService) executor : null;
this.completionQueue = new LinkedBlockingQueue<Future<V>>();
}

任务的提交和执行都是委托给Executor来完成。在构造函数中创建一个BlockingQueue来保存计算完成的结果,当提交某个任务时,该任务首先将被包装为一个QueueingFuture,

    public Future<V> submit(Callable<V> task) {
if (task == null) throw new NullPointerException();
RunnableFuture<V> f = newTaskFor(task);
executor.execute(new QueueingFuture(f));
return f;
}

QueueingFuture,这是FutureTask的一个子类,通过改写该子类的done方法,可以实现当任务完成时,将结果放入到BlockingQueue中。

private class QueueingFuture extends FutureTask<Void> {

QueueingFuture(RunnableFuture<V> task) {

super(task, null);

this.task = task;

}

protected void done() { completionQueue.add(task); }

private final Future<V> task;

}

而通过使用BlockingQueue的take(阻塞获取)或poll(非阻塞获取)方法,则可以得到结果。在BlockingQueue不存在元素时,这两个操作会阻塞,一旦有结果加入,则立即返回。

附加知识点:

take():取走BlockingQueue里排在首位的对象,若BlockingQueue为空,阻断进入等待状态直到Blocking有新的对象被加入为止;

poll(time):取走BlockingQueue里排在首位的对象,若不能立即取出,则可以等time参数规定的时间,取不到时返回nul

public Future<V> take() throws InterruptedException {
return completionQueue.take();
} public Future<V> poll() {
return completionQueue.poll();
}

Future 和 ExecutorCompletionService 对比和使用的更多相关文章

  1. 谈谈new Thread的弊端及Java四种线程池的使用

    1.new Thread的弊端执行一个异步任务你还只是如下new Thread吗? new Thread(new Runnable() { @Override public void run() { ...

  2. JAVA多线程提高七:Callable与Future的应用

    Callable与Runnable 先说一下java.lang.Runnable吧,它是一个接口,在它里面只声明了一个run()方法: public interface Runnable { publ ...

  3. JAVA多线程学习十-Callable与Future的应用

    Callable与Runnable 先说一下java.lang.Runnable吧,它是一个接口,在它里面只声明了一个run()方法: public interface Runnable { publ ...

  4. 九 fork/join CompletableFuture

    1: Fork/join fork/join:  fork是分叉的意思, join是合并的意思. Fork/Join框架:是JAVA7提供的一个用于并行执行任务的框架,是一个把大任务分割成若干个小任务 ...

  5. Java线程之CompletionService批处理任务

    如果你向Executor提交了一个批处理任务,并且希望在它们完成后获得结果,怎么办呢? 为此你可以保存与每个任务相关联的Future,然后不断地调用 timeout为零的get,来检验Future是否 ...

  6. 《java并发编程实战》读书笔记5--任务执行, Executor框架

    第6章 任务执行 6.1 在线程中执行任务 第一步要找出清晰的任务边界.大多数服务器应用程序都提供了一种自然的任务边界选择方式:以独立的请求为边界. -6.6.1 串行地执行任务 最简单的任务调度策略 ...

  7. 使用CompletionService批处理任务(线程池阻塞线程)

    CompletionService ExecutorService BlockingQueueFuture 如果你向Executor提交了一个批处理任务,并且希望在它们完成后获得结果.为此你可以保存与 ...

  8. 【并发那些事】线程有序化神器CompletionService

    前言 话说有一天,产品经理突然找到正在摸鱼的你. 产品:『我们要加一个聚合搜索功能,当用户在我们网站查询一件商品时,我们分别从 A.B.C 三个网站上查询这个信息,然后再把得到的结果返回给用户』 你: ...

  9. java中CompletionService的使用

    java中CompletionService的使用 之前的文章中我们讲到了ExecutorService,通过ExecutorService我们可以提交一个个的task,并且返回Future,然后通过 ...

随机推荐

  1. curl_redir_exec()函数

    function curl_redir_exec($ch,$debug="") { static $curl_loops = 0; static $curl_max_loops = ...

  2. The operator == is undefined for the argument type(s) int, null

    package cn.edu.shu.web.test; public class TestInteger { public static void main(String[] args) { /** ...

  3. MyEclipse新建Web Project报错

    1.详细报错例如以下图 2.报错原因 3.解决方法

  4. ASP.NET WebForm路由模拟

    一.ASP.NET MVC 路由(一)--- ASP.NET WebForm路由模拟 2014-11-08 11:49 by 郝喜路, 232 阅读, 0 评论, 收藏, 编辑 ASP.NET Web ...

  5. 用HTML和javascript(JS)计算触屏手机手指滑动方向的演示

    移动终端的流行,程序员希望通过HTML+JS完成触屏动作的识别.下面给出具体实现的例子,供大家参考. 将下面的代码复制并保存,用手机访问,现在的手机浏览器一般都支持触屏,针对本演示来讲就是支持三个js ...

  6. Andy Williams 《Love Story》

    where do i beginto tell a story of how great a love can bethe sweet love story that is older than th ...

  7. Hibernate在自由状态和持久的状态转变

    在Hibernate在.一PO术后可能长时间,session过时关闭.此时PO它一直是游离状态的对象,在这种状态下,以被转换成持久战,有几种方法如下: 1.session.saveOrUpdate(o ...

  8. JDK6、Oracle11g、Weblogic10 For Linux64Bit安装部署说明

    JDK6.Oracle11g.Weblogic10 For Linux64Bit安装部署说明 项目编号 编写人 成 编写日期 2013/07/29 审核 修订说明 目录 JDK6.ORACLE11G. ...

  9. Oracle wrap 和 unwrap( 加密与解密) 说明

    一. Wrap 说明 官网的说明如下: A PL/SQL Source Text Wrapping http://download.oracle.com/docs/cd/E11882_01/appde ...

  10. ACE模板之Jqgrid

    Asp.Net MVC中使用ACE模板之Jqgrid   第一次看到ACE模板,有种感动,有种相见恨晚的感觉,于是迅速来研究.它本身是基于bootstrap和jqueryui,但更nice,整合之后为 ...