Executor框架

   Executor框架是指java5中引入的一系列并发库中与executor相关的功能类,包括Executor、Executors、ExecutorService、CompletionService、Future、Callable等。(图片引用自http://www.javaclubcn.com/a/jichuzhishi/2012/1116/170.html)。

1.Executors类,提供了一系列工厂方法用于创先线程池,返回的线程池都实现了ExecutorService接口。

  public static ExecutorService newFixedThreadPool(int nThreads)

  创建固定数目线程的线程池。

  public static ExecutorService newCachedThreadPool()

  创建一个可缓存的线程池,调用execute 将重用以前构造的线程(如果线程可用)。如果现有线程没有可用的,则创建一个新线程并添加到池中。终止并从缓存中移除那些已有 60 秒钟未被使用的线程。

  public static ExecutorService newSingleThreadExecutor()

  创建一个单线程化的Executor。

  public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize)

  创建一个支持定时及周期性的任务执行的线程池,多数情况下可用来替代Timer类。

2.Executor

  Executor接口是Executor框架中最基础的部分,定义了一个用于执行Runnable的execute方法。它没有直接的实现类,有一个重要的子接口ExecutorService。

3.ExecutorService

  Executor接口是Executor框架中最基础的部分,定义了一个用于执行Runnable的execute方法。它没有直接的实现类,有一个重要的子接口ExecutorService。

  ExecutorService扩展了Executor并添加了一些生命周期管理的方法。一个Executor的生命周期有三种状态,运行 ,关闭 ,终止 。Executor创建时处于运行状态。当调用ExecutorService.shutdown()后,处于关闭状态,isShutdown()方法返回true。这时,不应该再想Executor中添加任务,所有已添加的任务执行完毕后,Executor处于终止状态,isTerminated()返回true。

 1 //继承自Executor接口
 2 public interface ExecutorService extends Executor {
 3     /**
 4      * 关闭方法,调用后执行之前提交的任务,不再接受新的任务
 5      */
 6     void shutdown();
 7     /**
 8      * 从语义上可以看出是立即停止的意思,将暂停所有等待处理的任务并返回这些任务的列表
 9      */
10     List<Runnable> shutdownNow();
11     /**
12      * 判断执行器是否已经关闭
13      */
14     boolean isShutdown();
15     /**
16      * 关闭后所有任务是否都已终止
17      */
18     boolean isTerminated();
19     /**
20      * 中断
21      */
22     boolean awaitTermination(long timeout, TimeUnit unit)
23         throws InterruptedException;
24     /**
25      * 提交一个Callable任务
26      */
27     <T> Future<T> submit(Callable<T> task);
28     /**
29      * 提交一个Runable任务,result要返回的结果
30      */
31     <T> Future<T> submit(Runnable task, T result);
32     /**
33      * 提交一个任务
34      */
35     Future<?> submit(Runnable task);
36     /**
37      * 执行所有给定的任务,当所有任务完成,返回保持任务状态和结果的Future列表
38      */
39     <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
40         throws InterruptedException;
41     /**
42      * 执行给定的任务,当所有任务完成或超时期满时(无论哪个首先发生),返回保持任务状态和结果的 Future 列表。
43      */
44     <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks,
45                                   long timeout, TimeUnit unit)
46         throws InterruptedException;
47     /**
48      * 执行给定的任务,如果某个任务已成功完成(也就是未抛出异常),则返回其结果。
49      */
50     <T> T invokeAny(Collection<? extends Callable<T>> tasks)
51         throws InterruptedException, ExecutionException;
52     /**
53      * 执行给定的任务,如果在给定的超时期满前某个任务已成功完成(也就是未抛出异常),则返回其结果。
54      */
55     <T> T invokeAny(Collection<? extends Callable<T>> tasks,
56                     long timeout, TimeUnit unit)
57         throws InterruptedException, ExecutionException, TimeoutException;
58 }

ExecutorService接口继承自Executor接口,定义了终止、提交任务、跟踪任务返回结果等方法。

    ExecutorService涉及到Runnable、Callable、Future接口,这些接口的具体内容如下。

 1 // 实现Runnable接口的类将被Thread执行,表示一个基本的任务
 2 public interface Runnable {
 3     // run方法就是它所有的内容,就是实际执行的任务
 4     public abstract void run();
 5 }
 6 // Callable同样是任务,与Runnable接口的区别在于它接收泛型,同时它执行任务后带有返回内容
 7 public interface Callable<V> {
 8     // 相对于run方法的带有返回值的call方法
 9     V call() throws Exception;
10 }
Future
 1 // Future代表异步任务的执行结果
 2 public interface Future<V> {
 3 
 4     /**
 5      * 尝试取消一个任务,如果这个任务不能被取消(通常是因为已经执行完了),返回false,否则返回true。
 6      */
 7     boolean cancel(boolean mayInterruptIfRunning);
 8 
 9     /**
10      * 返回代表的任务是否在完成之前被取消了
11      */
12     boolean isCancelled();
13 
14     /**
15      * 如果任务已经完成,返回true
16      */
17     boolean isDone();
18 
19     /**
20      * 获取异步任务的执行结果(如果任务没执行完将等待)
21      */
22     V get() throws InterruptedException, ExecutionException;
23 
24     /**
25      * 获取异步任务的执行结果(有最常等待时间的限制)
26      *
27      *  timeout表示等待的时间,unit是它时间单位
28      */
29     V get(long timeout, TimeUnit unit)
30         throws InterruptedException, ExecutionException, TimeoutException;
31 }

4.ScheduledExecutorService

可以安排指定时间或周期性的执行任务的ExecutorService。
 
 1 //指定延时delay之后,执行command
  public ScheduledFuture<?> schedule(Runnable command, 2 long delay, TimeUnit unit); 3 //同上 4 public <V> ScheduledFuture<V> schedule(Callable<V> callable, 5 long delay, TimeUnit unit);
  创建并执行一个周期性的任务,初始延时为InitialDelay,然后每隔period之后,执行该任务。
6 public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, 7 long initialDelay, 8 long period, 9 TimeUnit unit); 10 public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, 11 long initialDelay, 12 long period, 13 TimeUnit unit);

 未完待续。。。。。。

【参考】http://blog.csdn.net/minword/article/details/20565867
    http://www.cnblogs.com/shipengzhi/articles/2067154.html
原文地址:https://www.cnblogs.com/plxx/p/5321380.html