美文网首页java多线程
多线程-源码解读Executor(执行器)

多线程-源码解读Executor(执行器)

作者: 余生爱静 | 来源:发表于2021-05-11 01:26 被阅读0次
    /**
     * An object that executes submitted {@link Runnable} tasks.
     Exector是一个执行已提交的可运行任务的对象
    
     * This interface provides a way of decoupling task submission  from the mechanics of how each task will be run, including details of thread use, scheduling, etc.
    从线程使用和调度等相关的运行机制里面解耦任务执行的一种方式
    
     *  An {@code Executor} is normally used instead of explicitly creating threads.
     * For  example, rather than invoking {@code new  Thread(new(RunnableTask())).start()} for each
     of a set of tasks, you might use:
    通常使用Executor代替显式创建线程,而不是为每个组任务调用线程的start方法
     *
     * <pre>
     * Executor executor = <em>anExecutor</em>;
     * executor.execute(new RunnableTask1());
     * executor.execute(new RunnableTask2());
     * ...
     * </pre>
     *
     * However, the {@code Executor} interface does not strictly  require 
    * that execution be asynchronous.
    但是,Executor接口并不严格要求执行是异步的。
    
     * In the simplest case, an  executor can run the submitted task immediately
    * in the caller's thread:
    最简单的情况,Executor可以在调用者的线程中立即运行提交的任务
     *
     *  <pre> {@code
     * class DirectExecutor implements Executor {
     *   public void execute(Runnable r) {
     *     r.run();
     *   }
     * }}</pre>
     *
     * More typically, tasks are executed in some thread other
     * than the caller's thread.  The executor below spawns a new thread
     * for each task.
    更典型地,任务在其他线程中而不是在调用者的线程中执行。
    
     *
     *  <pre> {@code
     * class ThreadPerTaskExecutor implements Executor {
     *   public void execute(Runnable r) {
     *     new Thread(r).start();
     *   }
     * }}</pre>
     *
     * Many {@code Executor} implementations impose some sort of
     * limitation on how and when tasks are scheduled. 
    许多Exector的实现者在任务怎样Scheduled以及何时Scheduled中强加了一些限制。
    
    * The executor below
     * serializes the submission of tasks to a second executor,
     * illustrating a composite executor.
     *  <pre> {@code
     * class SerialExecutor implements Executor {
     *   final Queue<Runnable> tasks = new ArrayDeque<Runnable>();
     *   final Executor executor;
     *   Runnable active;
     *
     *   SerialExecutor(Executor executor) {
     *     this.executor = executor;
     *   }
     *
     *   public synchronized void execute(final Runnable r) {
     *     tasks.offer(new Runnable() {
     *       public void run() {
     *         try {
     *           r.run();
     *         } finally {
     *           scheduleNext();
     *         }
     *       }
     *     });
     *     if (active == null) {
     *       scheduleNext();
     *     }
     *   }
     *
     *   protected synchronized void scheduleNext() {
     *     if ((active = tasks.poll()) != null) {
     *       executor.execute(active);
     *     }
     *   }
     * }}</pre>
     *
     * The {@code Executor} implementations provided in this package
     * implement {@link ExecutorService}, which is a more extensive interface. 
      实现了ExecutorService的Executor实现者是使用更广泛的
    
     *The {@link ThreadPoolExecutor} class provides an
     * extensible thread pool implementation. 
    ThreadPoolExecutor类提供可扩展的线程池实现
    
    * The {@link Executors} class
     * provides convenient factory methods for these Executors.
    Executors类为这些执行器提供了方便的工厂方法
    
     *
     * <p>Memory consistency effects: Actions in a thread prior to
     * submitting a {@code Runnable} object to an {@code Executor}
     * <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a>
     * its execution begins, perhaps in another thread.
     *
     * @since 1.5
     * @author Doug Lea
     */
    public interface Executor {
    
        /**
         * Executes the given command at some time in the future. 
           在将来的某个时间执行给定的命令。
    
         *  The command
         * may execute in a new thread, in a pooled thread, or in the calling
         * thread, at the discretion of the {@code Executor} implementation.
         这个命令将会根据Executor的不同实现执行在一个新线程,或者一个线程池中、或者调用的线程中。
         *
         * @param command the runnable task
         * @throws RejectedExecutionException if this task cannot be
         * accepted for execution
         * @throws NullPointerException if command is null
         */
        void execute(Runnable command);
    }
    

    结论

    执行器的作用如下:
    1、从线程的使用和调度机制中解耦任务的执行
    2、代替线程的显示创建
    3、执行器没有严格要求一定是异步的,这个完全取决于它的实现类。

    相关文章

      网友评论

        本文标题:多线程-源码解读Executor(执行器)

        本文链接:https://www.haomeiwen.com/subject/rugjdltx.html