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

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接口用法总结

    主要介绍了Java中Executor接口用法,较为详细的总结了Executor接口的定义、创建及用法,需要的朋友可以参考下

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

    Executor: 一个接口,其定义了一个接收Runnable对象的方法executor,其方法签名为executor(Runnable command),该方法接收一个Runable实例,它用来执行一个任务,任务即一个实现了Runnable接口的类,一般来说,...

    线程池核心组件源码剖析.docx

    该组件中,Executor 和 ExecutorService 接口 定义了线程池最核心的几个方法,提交任务 submit ()、关闭线程池 shutdown()。抽象类 AbstractExecutorService 主要对公共行为 submit()系列方法进行了实现,这些 ...

    mybatis分页源码手写分页

    并通过executor和autoCommit等参数实例化SqlSession接口的实现类DefaultSqlSession。DefaultSqlSession具体实现了SqlSession中...并通过自己持有的Executor接口,委托具体的Executor去执行sql语句完成具体的CRUD操作。

    线程超时死掉

    其中Future 接口就是订货单,真正处理订单的是Executor类,它根据Future接口的要求来生产产品。 Future接口提供方法来检测任务是否被执行完,等待任务执行完获得结果,也可以设置任务执行的超时时间。这个设置超时...

    Java面试问题带答案40道.docx

    另外,还可以使用Executor框架或线程池来管理线程。 3. Java中什么是抽象类? 答:抽象类是一种不能被实例化的类,只能作为其他类的基类。它可以包含抽象方法,用于定义子类必须实现的方法。 4. Java中什么是接口...

    iBatis框架搭建用到的所有jar包

    程序员往往只需定义好了POJO 到数据库表的映射关系,即可通过 Hibernate或者OJB 提供的方法完成持久层操作。程序员甚至不需要对 SQL 的熟练掌握,Hibernate/OJB 会根据制定的存储逻辑,自动生成对应的 SQL 并调用 ...

    powershell-command-executor:node.js模块,通过长期建立的远程PSSession提供注册表和网关,用于执行powershell命令

    Node.js模块,通过长期建立的远程PSSession,提供注册表和网关来执行预定义的powershell命令。 总览 这个Node.js模块建立在之上,为预定义命令的注册表提供了更高级别的API,特别是针对Office365的各种powershell...

    spring.net中文手册在线版

    利用executor执行并行的grep 25.6.AOP 第二十六章. AOP指南 26.1.简介 26.2.基础知识 26.2.1.应用通知 26.2.2.使用切入点-基本概念 26.3.深入探讨 26.3.1.其它类型的通知 26.3.1.1.前置通知 26.3.1.2.后置通知 26.3...

    \java超强笔记(超级经典)

    Executor接口: 替代了Thread类,他可以创建定量的、动态的以及周期性的线程池。 ExecutorService接口: 线程池,用来存放线程来节省创建和销毁资源的消耗。 Callable和Future接口: Callable是类似于...

    第7章-JUC多线程v1.1.pdf

    线程池的顶层接口是Executor, 这个接口定义了一个核心方法executor(Runnable command), 这个方法最后被ThreadPoolExecutor类实现, 这个方法用来传入任务, 并且该类是线程池的核心类, 构造方法如下 : public ...

    python程序中的线程操作 concurrent模块使用详解

    ProcessPoolExecutor 和 ThreadPoolExecutor:两者都实现相同的接口,该接口由抽象Executor类定义。 二、基本方法 submit(fn, *args, **kwargs) :异步提交任务 map(func, *iterables, timeout=None, chunksize=1) ...

    jdav-client:Java 的 DAV 客户端

    示例中使用的 Executor 需要实现 http-client-interfaces 库中的 IHttpExecutor 接口。 Executor 实现不是这个库的一部分。 // create a DavContext. This needs to be done only once.// Multiple requests can ...

    Java JDK 7学习笔记(国内第一本Java 7,前期版本累计销量5万册)

    7.1.1 接口定义行为 200 7.1.2 行为的多态 204 7.1.3 解决需求变化 206 7.2 接口语法细节 213 7.2.1 接口的默认 213 7.2.2 匿名内部类 217 7.2.3 使用enum枚举常数 221 7.3 重点复习 224 7.4 课后...

    Spring.3.x企业应用开发实战(完整版).part2

    4.11.1 使用Java类提供Bean定义信息 4.11.2 使用基于Java类的配置信息启动Spring容器 4.12 不同配置方式比较 4.13 小结 第5章 Spring容器高级主题 5.1 Spring容器技术内幕 5.1.1 内部工作机制 5.1.2 BeanDefinition ...

    Spring3.x企业应用开发实战(完整版) part1

    4.11.1 使用Java类提供Bean定义信息 4.11.2 使用基于Java类的配置信息启动Spring容器 4.12 不同配置方式比较 4.13 小结 第5章 Spring容器高级主题 5.1 Spring容器技术内幕 5.1.1 内部工作机制 5.1.2 BeanDefinition ...

    earth-frost任务调度框架-其他

    frost-core 定义调度、执行、注册发现等核心功能的接口和实现 frost-center 调度中心服务,包含安全认证和UI展示,依赖core实现调度逻辑 frost-executor 执行器服务,依赖core实现任务执行逻辑,包含开发脚本任务的...

    springmybatis

    在User.xml 的配置文件中,mapper namespace="com.yihaomen.mybatis.inter.IUserOperation" ,命名空间非常重要,不能有错,必须与我们定义的package 和 接口一致。 运行这个测试程序,就可以看到结果了。 除非...

    浅谈Android开发系列网络篇之Retrofit

    Retrofit是一个不错的网络请求库,用官方自己的介绍就是: ...首先定义请求接口,即程序中都需要什么请求操作 public interface GitHubService { @GET(/users/{user}/repos) List&lt;Repo&gt; listRepos(@Path

    Java常见面试题208道.docx

    14.接口和抽象类有什么区别? 15.java 中 IO 流分为几种? 16.BIO、NIO、AIO 有什么区别? 17.Files的常用方法都有哪些? 二、容器 18.java 容器都有哪些? 19.Collection 和 Collections 有什么区别? 20.List、Set...

Global site tag (gtag.js) - Google Analytics