码迷,mamicode.com
首页 > 其他好文 > 详细

并发编程:任务执行

时间:2017-04-09 00:17:23      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:task   sub   并发编程   使用   报告   定时线程   time   run   方式   

1、每当看到下面这种代码 new Thread(runnable).start() 时,请考虑用Executor

2、Executor

  1)创建方式:Executors.new..

  2)通过使用Executor,可以实现各种调优、管理、监视、记录日志、错误报告

  3)Executor有4个生命周期:创建、提交、开始和完成

  4)ExecutorService: extends Executor,有三种状态:运行、关闭、终止

  5)支持限时的invokeAll,将多个任务提到ExecutorService并获得结果

3、Timer: timer.schedule(runnable, timout)

  1)Timer在执行所有定时任务时只会创建一个线程

  2)Timer线程并不捕获异常,因此当TimerTask抛出未检查的异常时将终止定时线程

4、Callable与Future

  1)Runnable与Callable描述的都是抽象的计算任务

  2)Future表示一个任务的生命周期,

    提供 cancel、isCancelled、isDone、get/get(timeout,timeunit)

Callable<V> task = new Callable<V>() {
  public V call() {
    return V;
  }
};    


5、CompletionService

CompletionService<V> completionService = new ExecutorCompletionService<V>(executor);
completionService.submit(callable)
completionService.take()

并发编程:任务执行

标签:task   sub   并发编程   使用   报告   定时线程   time   run   方式   

原文地址:http://www.cnblogs.com/hellocyc/p/6683349.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!