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

    Executor框架-1-Executor接口

    Executor是一个接口,Executor框架的基础,它将任务的提交与任务的执行分离。Executor的功能,看源码注释内容基本就可以了解了;

    
    
    package java.util.concurrent;
    
    /**
     * Executor是执行提交任务的类;
     * 这个接口提供了将任务提交与任务运行分离的机制,屏蔽了运行时的线程调用、任务安排等细节;通常使用
     * Executor执行任务,而不必显式的使用Thread为每个任务创建线程并执行任务。可以像下面示例那样使用Executor执行任务: 
     *  Executor executor = anExecutor();
     *  executor.execute(new RunnableTask1());
     *  executor.execute(new RunnableTask2());
     * 
     * 然后,Executor接口不是必须异步执行任务的,最简单的应用方法是,executor在当前线程中直接执行任务:
     *  class DirectExecutor implements Executor {
     *   public void execute(Runnable r) {
     *    r.run();
     *   }
     *  }}
     *  
     * 在更加典型的应用场景中,任务在其他线程中执行任务而不是在当前线程,executor可以为每个任务派生一个新线程
     * class ThreadPerTaskExecutor implements Executor {
     *  public void execute(Runnable r) {
     *   new Thread(r).start();
     *  }
     * }}
     *  
     * 一些Executor实现类实现支持排序机制,可以决定在什么时候该怎么执行任务。
     * 在concurrent包中,提供了Execuor包的实现类ExecutorService,这个接口比Executor功能更丰富;
     * ThreadPoolExecutor实现了一个可扩展的线程池;Executors为这些Executor实现类提供了方便的工厂方法;
     *  
     * 内存一致性影响:一个Runnable提交给Executor必须执行在其执行之前,即使在另一个线程中;
     * @since 1.5
     * @author Doug Lea
     */
    public interface Executor {
    
      /**
    	 * 在未来的某个时间执行给定的任务,这个任务可能在一个新线程、线程池或当前线程中执行,
    	 * 具体执行方法取决于实现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);
    }
    
    
  • 相关阅读:
    嵌入式Linux操作系统学习规划
    底层机器指令学习
    汇编学习笔记
    无符号和有符号数操作优先级
    栈和堆的区别
    图Graph
    判断单链表里面有没有环
    centos配置中文显示和中文输入
    数组相关问题求解
    KMP算法
  • 原文地址:https://www.cnblogs.com/donfaquir/p/13941450.html
Copyright © 2011-2022 走看看