Executor

来源:互联网 发布:软件汉化工具 编辑:程序博客网 时间:2024/06/11 18:41

All Known Subinterfaces:
ExecutorService, ScheduledExecutorService
All Known Implementing Classes:
AbstractExecutorService, ForkJoinPool, ScheduledThreadPoolExecutor, ThreadPoolExecutor


public interface Executor

An object that executes submitted 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 Executor is normally used instead(代替) of explicitly(明确) creating threads. For example, rather than(而不是) invoking new Thread(new(RunnableTask())).start() for each of a set of tasks(而不是为一组任务调用新的), you might use:

 Executor executor = anExecutor; executor.execute(new RunnableTask1()); executor.execute(new RunnableTask2()); ...

However, the Executor 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:

 class DirectExecutor implements Executor {   public void execute(Runnable r) {     r.run();   } }

More typically, tasks are executed in some thread other than the caller’s thread. The executor below(下面) spawns(大量生产) a new thread for each task.

 class ThreadPerTaskExecutor implements Executor {   public void execute(Runnable r) {     new Thread(r).start();   } }

Many Executor 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(说明复合执行器。).

 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);     }   } }

The Executor implementations provided in this package implement ExecutorService, which is a more extensive interface(这是一个更广泛的Interface). The ThreadPoolExecutor class provides an extensible(扩展) thread pool implementation. The Executors class provides convenient(方便) factory methods for these Executors.
Memory consistency(一致性) effects(效应): Actions in a thread prior(预先) to submitting a Runnable object to an Executor happen-before(发生-前) its execution begins, perhaps(或者) in another thread.

Since:
1.5

Method Detail

execute

void execute(Runnable command)

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 Executor implementation.
Parameters:
command - the runnable task
Throws:
RejectedExecutionException - if this task cannot be accepted for execution
NullPointerException - if command is null

原创粉丝点击