zoukankan      html  css  js  c++  java
  • Executor框架-2-ExecutorServcie接口

    ExecutorService是Java中对线程池定义的一个接口,它java.util.concurrent包中,在这个接口中定义了和后台任务执行相关的方法,具体的解释可以看下面的源码注释:

    
    package java.util.concurrent;
    
    import java.util.Collection;
    import java.util.List;
    
    /**
     * Executor子类,提供结束任务执行的函数,同时提供可以返回Future类的函数,Future用于跟踪一个或多个异步任务执行进度;
     * 
     * ExecutorService可以被主动关闭自己,结束状态的ExecutorService不会接收新任务;
     * ExecutorService提供两个方法用于主动结束自己,shutdown函数允许在终止之前执行以前提交的任务;
     * shutdownNow会阻止正在等待的任务进入执行状态,并且会尝试停止正在执行的任务;
     * ExecutorService终止时,没有正在执行的任务,没有等待的任务,不能提交新任务;应关闭未使用的ExecutorService以允许回收其资源。
     * 
     * submit函数继承Executor.execute方法,并创建并返回一个Future,Future可以用于取消任务执行或等待任务执行完毕;
     * invokeAny和invokeAll可以用于批量执行任务(执行一组任务),然后等待一个或所有任务执行完毕;
     * ExecutorCompletionService类可用于批量执行任务,提供了上述方法的其他形式的实现;
     * 
     * Executors类为ExecutorService的实现类提供了工厂方法;
     * 
     * 内存一致性效应:
     * 任务任何操作必须发生在通过Future获取结果之前,
     * 任务提交到ExecutorService发生在任务任何操作之前
    
     * @since 1.5
     * @author Doug Lea
     */
    public interface ExecutorService extends Executor {
    
      /**
    	 * 启动有序关闭,允许执行以前提交的任务,但不接受任何新任务。
    	 * 如果已经关闭,则调用此函数没有其他效果。
    	 * 
    	 * 这个方法不会等待之前提交的任务完成执行,awaitTermination函数运行等待之前提交的任务完成,然后关闭;
       */
      void shutdown();
    
      /**
    	 * 尝试关闭所有正在执行的任务,暂停所有正在等待执行的任务,以列表的形式返还这些等待任务;
    	 * 
    	 * 这个函数不等待执行的任务执行完毕,可以调用awaitTermination等待正在执行的任务完毕;
       * 
    	 * 这个函数不保证一定会终止正在运行的任务;例如此方法的一种典型的实现是使用
    	 * {@link Thread#interrupt}终止任务,那么,任何不响应线程中断的任务都将不会被终止;
       */
      List<Runnable> shutdownNow();
    
      /**
       * 返回当前executor是否被终止
       */
      boolean isShutdown();
    
      /**
    	 * 如果executor关闭后所有任务均已完成,则返回 true;
    	 * 所以,如果没有调用shutdown或shutdownNow函数,isTerminated返回结果永远为false;
       */
      boolean isTerminated();
    
      /**
    	 * 调用此方法后,executor会阻塞,直到以下三种情况发生后,才关闭executor:
    	 * . 所有的任务都执行完毕;
    	 * . 等待超时;
    	 * . 线程被中断;
       */
      boolean awaitTermination(long timeout, TimeUnit unit)
        throws InterruptedException;
    
      /**
    	 * 提交一个返回结果的任务,返回Future获取任务执行进度。一旦任务成功完成,可以通过Future的get方法获取任务的返回值;
    	 * 
    	 * 如果你想立即阻塞当前线程以等待任务返回值,可以使用如下调用方式:
    	 * result = exec.submit(aCallable).get(); //Future会阻塞调用线程,直到有返回结果;
    	 * 
    	 * 注意:{@link Executors}类包括一组方法,可以将其他一些常见的类似闭包的对象
    	 * (例如,{@link java.security.PrivilegedAction})转换为{@link Callable}形式,以便可以提交。
    	 
    	 * @param task the task to submit
       * @param <T> the type of the task's result
       * @return a Future representing pending completion of the task
       * @throws RejectedExecutionException if the task cannot be
       *     scheduled for execution
       * @throws NullPointerException if the task is null
       */
      <T> Future<T> submit(Callable<T> task);
    
      /**
    	 * 提交一个Runnable任务,返回Future代表任务,如果任务成功执行,Future的get函数会返回给定的返回值
       * @param task the task to submit
       * @param result the result to return
       * @param <T> the type of the result
       * @return a Future representing pending completion of the task
       * @throws RejectedExecutionException if the task cannot be
       *     scheduled for execution
       * @throws NullPointerException if the task is null
       */
      <T> Future<T> submit(Runnable task, T result);
    
      /**
    	 * 提交一个Runnable任务,返回Future代表任务,如果任务成功执行,Future的get函数会返回null
       * @param task the task to submit
       * @return a Future representing pending completion of the task
       * @throws RejectedExecutionException if the task cannot be
       *     scheduled for execution
       * @throws NullPointerException if the task is null
       */
      Future<?> submit(Runnable task);
    
      /**
    	 * 执行给定的所有任务,如果任务都执行完毕,返回一个Future列表,此列表保存这些任务的状态和结果;
    	 * 列表中每个Future节点的isDone函数返回值都是true;
    	 * 
    	 * 请注意,完成的任务可能正常终止,也可能因引发异常而终止。
    	 * 如果在此操作进行期间修改给定集合,则此方法的结果是未知的。
       * @param tasks the collection of tasks
       * @param <T> the type of the values returned from the tasks
       * @return a list of Futures representing the tasks, in the same
       *     sequential order as produced by the iterator for the
       *     given task list, each of which has completed
       * @throws InterruptedException if interrupted while waiting, in
       *     which case unfinished tasks are cancelled
       * @throws NullPointerException if tasks or any of its elements are {@code null}
       * @throws RejectedExecutionException if any task cannot be
       *     scheduled for execution
       */
      <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
        throws InterruptedException;
    
      /**
    	 * 执行给定的所有任务,当所有任务执行完毕或执行超时,返回一个Future列表,此列表保存这些任务的状态和结果;
    	 * 列表中每个Future节点的isDone函数返回值都是true;
    	 * 此方法一旦返回(超时的情况),那些未执行的任务会被取消;
    	 * 请注意,完成的任务可能正常终止,也可能因引发异常而终止。
    	 * 如果在此操作进行期间修改给定集合,则此方法的结果是未知的
       * @param tasks the collection of tasks
       * @param timeout the maximum time to wait
       * @param unit the time unit of the timeout argument
       * @param <T> the type of the values returned from the tasks
       * @return a list of Futures representing the tasks, in the same
       *     sequential order as produced by the iterator for the
       *     given task list. If the operation did not time out,
       *     each task will have completed. If it did time out, some
       *     of these tasks will not have completed.
       * @throws InterruptedException if interrupted while waiting, in
       *     which case unfinished tasks are cancelled
       * @throws NullPointerException if tasks, any of its elements, or
       *     unit are {@code null}
       * @throws RejectedExecutionException if any task cannot be scheduled
       *     for execution
       */
      <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks,
                     long timeout, TimeUnit unit)
        throws InterruptedException;
    
      /**
    	 * 执行给定的任务,如果有成功完成的任务,则返回已成功完成的任务的结果(不包括抛出异常的任务)。
    	 * 此方法正常或异常返回后,未完成的任务将被取消。
    	 * 如果在此操作进行期间修改给定集合,则此方法的结果是未定义的。
       * @param tasks the collection of tasks
       * @param <T> the type of the values returned from the tasks
       * @return the result returned by one of the tasks
       * @throws InterruptedException if interrupted while waiting
       * @throws NullPointerException if tasks or any element task
       *     subject to execution is {@code null}
       * @throws IllegalArgumentException if tasks is empty
       * @throws ExecutionException if no task successfully completes
       * @throws RejectedExecutionException if tasks cannot be scheduled
       *     for execution
       */
      <T> T invokeAny(Collection<? extends Callable<T>> tasks)
        throws InterruptedException, ExecutionException;
    
      /**
    	 * 执行给定的任务,如果在指定的时间内有成功完成的任务,则返回已成功完成的任务的结果(不包括抛出异常的任务)。
    	 * 此方法正常或异常返回后,未完成的任务将被取消。
    	 * 如果在此操作进行期间修改给定集合,则此方法的结果是未定义的。
       * @param tasks the collection of tasks
       * @param timeout the maximum time to wait
       * @param unit the time unit of the timeout argument
       * @param <T> the type of the values returned from the tasks
       * @return the result returned by one of the tasks
       * @throws InterruptedException if interrupted while waiting
       * @throws NullPointerException if tasks, or unit, or any element
       *     task subject to execution is {@code null}
       * @throws TimeoutException if the given timeout elapses before
       *     any task successfully completes
       * @throws ExecutionException if no task successfully completes
       * @throws RejectedExecutionException if tasks cannot be scheduled
       *     for execution
       */
      <T> T invokeAny(Collection<? extends Callable<T>> tasks,
              long timeout, TimeUnit unit)
        throws InterruptedException, ExecutionException, TimeoutException;
    }
    
    
  • 相关阅读:
    [已解决] MAVEN安装代码到本地库,安装jar, source, javadoc的方式
    [已解决]Eclipse 插件Maven在使用 add dependency,找不到包,解决办法
    [已解决] windows 下 git 免输密码
    [已解决] windows 80端口被占用
    [已解决]Tomcat启动报 java.net.BindException: Address already in use: JVM_Bind
    [已解决] java.net.InetAddress.getHostName() 阻塞问题
    [已解决] 日常开发中禁用Tomcat自动重启
    [已解决] MyBatis 中bind用法
    [转]SOCKET通信中TCP、UDP数据包大小的确定
    使用beautifulsoup与requests爬取数据
  • 原文地址:https://www.cnblogs.com/donfaquir/p/13941462.html
Copyright © 2011-2022 走看看