`
Donald_Draper
  • 浏览: 972090 次
社区版块
存档分类
最新评论

Executor接口的定义

    博客分类:
  • JUC
阅读更多
package java.util.concurrent;

/**
 * An object that executes submitted {@link Runnable} tasks. 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 <tt>Executor</tt> is normally used
 * instead of explicitly creating threads. For example, rather than
 * invoking <tt>new Thread(new(RunnableTask())).start()</tt> for each
 * of a set of tasks, you might use:
 *
 Executor用于执行提交的Runnable任务。Executor提供了每个线程如何运行,线程的具体使用调度
 机制的解耦的一种方式。一般用Executor执行线程,而不是显示地创建线程。比如
 调用new Thread(new(RunnableTask())).start()执行线程任务。我们可以用以下方式执行线程:
 * <pre>
 * Executor executor = [i]anExecutor[/i];
 * executor.execute(new RunnableTask1());
 * executor.execute(new RunnableTask2());
 * ...
 * </pre>
 *
 * However, the <tt>Executor</tt> interface does not strictly
 * require that execution be asynchronous. In the simplest case, an
 * executor can run the submitted task immediately in the caller's
 * thread:
 *
Executor不需要严格意义上地异步执行线程。在一些简单的例子中,executor可以
立即在调用线程中,运行提交的任务。
 * <pre>
 * 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>
 * class ThreadPerTaskExecutor implements Executor {
 *     public void execute(Runnable r) {
 *         new Thread(r).start();
 *     }
 * }</pre>
 *
 * Many <tt>Executor</tt> implementations impose some sort of
 * limitation on how and when tasks are scheduled.  The executor below
 * serializes the submission of tasks to a second executor,
 * illustrating a composite executor.
 *一些执行器用于实现任务何时如何调度,下面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 <tt>Executor</tt> implementations provided in this package
 * implement {@link ExecutorService}, which is a more extensive
 * interface.  The {@link ThreadPoolExecutor} class provides an
 * extensible thread pool implementation. The {@link Executors} class
 * provides convenient factory methods for these Executors.
 *
ExecutorService提供了Executor实现,是一个广泛使用的接口。ThreadPoolExecutor为
一个可扩展线程池的实现。Executors提供了线程池执行器的便利的工厂方法。
 * <p>Memory consistency effects: Actions in a thread prior to
 * submitting a {@code Runnable} object to an {@code Executor}
 * [url=package-summary.html#MemoryVisibility]<i>happen-before</i>[/url]
 * 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 <tt>Executor</tt> 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);
}
0
0
分享到:
评论

相关推荐

    Java中Executor接口用法总结

    Executor接口定义了一个单一的方法`execute(Runnable command)`,用于提交一个Runnable任务到执行器,这个任务无需返回结果,但可以抛出未检查异常。 Executor接口本身并不创建或管理线程,而是作为一个调度者,它...

    03 mybatis高级(2) 2

    Executor接口定义了执行SQL的基本操作,包括简单查询、更新、删除等。MyBatis提供了三种Executor实现:SimpleExecutor、ReuseExecutor和BatchExecutor,分别对应不同的执行策略。SimpleExecutor每次执行都会创建新的...

    mybatis所有源码

    Executor接口定义了执行SQL的方法,如query和update等。 4. Mapper:MyBatis允许我们定义Mapper接口,接口方法与Mapper XML文件中的SQL语句对应。通过MapperProxy和MapperFactoryBean,MyBatis实现了接口方法调用与...

    Mybatis源码分析.docx

    在Mybatis中,执行器是由Executor接口定义的,该接口提供了 execute() 方法用于执行SQL语句。Mybatis提供了多种执行器实现,如SimpleExecutor、BatchExecutor等。 OpenSession OpenSession是Mybatis中的一种会话...

    并发编程72发.docx

    - Executor接口定义了任务执行的接口,ExecutorService扩展了Executor,提供了更丰富的方法监控和控制任务执行。 - Executors工具类提供预定义的线程池类型,如固定大小、缓存和单线程池等,方便根据业务需求选择...

    java并发工具包

    Executor接口定义了一个方法`execute(Runnable task)`,用于执行任务。ExecutorService是Executor的一个扩展,提供了一套更丰富的生命周期管理方法,如`submit()`、`shutdown()`和`awaitTermination()`等,方便管理...

    1_Executor源码阅读1

    ### Executor接口的优势 1. **任务创建与执行解耦** - 客户端只需关注任务的定义,无需关心如何运行任务。这增加了代码的可读性和可维护性,因为任务执行的逻辑被转移到`Executor`实现中。 2. **避免显式创建线程*...

    mybatis中的sqlsession--executor实现.zip

    `Executor`是`SqlSession`的执行引擎,它定义了对数据库的基本操作。MyBatis提供了两种基本的Executor实现: - **SimpleExecutor**:简单执行器,每个SQL语句都会创建一个新的Statement。这是默认的Executor,适用...

    Java Executor 框架的实例详解

    Executor 接口是 Executor 框架的核心接口,定义了 execute() 方法,用于提交 Runnable 任务。Executor 接口提供了一种标准的方式来提交任务,开发者可以使用 Executor 接口来提交任务,并由 Executor 框架来执行...

    Java-Executor并发框架.docx

    Executor接口是一个执行线程的工具,它定义了一个方法`execute(Runnable command)`,用于提交任务到线程池执行。然而,Executor本身并不是线程池,它只是一个调度任务的抽象。真正实现线程池功能的是ExecutorService...

    Executor,Executors,ExecutorService比较.docx

    总结来说,`Executor`定义了任务执行的基本行为,`Executors`是创建线程池的工厂,而`ExecutorService`则提供了管理和控制线程池的能力。在实际编程中,我们通常通过`Executors`创建线程池,然后利用`...

    线程池之Executor框架.docx

    - **任务执行**:核心接口`Executor`定义了任务的执行方法`execute()`,用于启动一个任务。`ExecutorService`接口继承自`Executor`,提供了更丰富的管理功能,如关闭线程池、管理线程等。`ThreadPoolExecutor`和`...

    java 中Executor, ExecutorService 和 Executors 间的不同

    这是一个抽象层面的核心接口,定义了 execute 方法,用于执行 Runnable 任务。与 java.lang.Thread 类不同的是,Executor 将任务本身和执行任务分离,可以阅读 difference between Thread and Executor 来了解 ...

    Java并发之线程池Executor框架的深入理解

    ExecutorService接口定义了对Executor的服务, ScheduledExecutorService接口定义了定时调度接口,而AbstractExecutorService抽象类则是执行框架的抽象实现。 ThreadPoolExecutor是JDK中线程池的具体实现,提供了一...

    并发编程之Executor线程池原理与源码解读.pdf

    而ExecutorService则是Executor接口的一个重要子接口,它扩展了Executor的功能,添加了管理线程池生命周期的方法。 至于线程的实现方式,通常有两种,即Runnable和Callable。Runnable接口是执行任务的最基本的实现...

    PyPI 官网下载 | executor-21.0.tar.gz

    1. **任务调度**:executor可能提供了灵活的任务调度机制,允许用户根据需要定义任务的执行顺序和时间。 2. **故障容错**:由于涉及分布式系统,executor可能具备一定的容错能力,当某个节点失败时,能够自动将任务...

    java并发编程:Executor、Executors、ExecutorService.docx

    Executor是一个接口,它定义了一个核心方法`executor(Runnable command)`,用于接收实现了Runnable接口的任务对象并执行。Executor的主要目的是抽象出线程的创建和管理,让开发者只需关注任务本身,而无需关心线程的...

    掌握并发的钥匙:Java Executor框架深度解析

    - **Executor**:一个接口,定义了执行提交的Runnable任务的方法。 - **Executors**:一个工厂类,用于创建不同类型的线程池。 - **ExecutorService**:一个接口,扩展了Executor,提供了额外的管理方法,如关闭...

Global site tag (gtag.js) - Google Analytics