Documentation

The Java™ Tutorials
Hide TOC
Executor Interfaces执行器接口
Trail: Essential Java Classes
Lesson: Concurrency
Section: High Level Concurrency Objects
Subsection: Executors

Executor Interfaces执行器接口

The java.util.concurrent package defines three executor interfaces:java.util.concurrent包定义了三个执行器接口:

Typically, variables that refer to executor objects are declared as one of these three interface types, not with an executor class type.通常,引用executor对象的变量被声明为这三种接口类型之一,而不是executor类类型。

The Executor InterfaceExecutor接口

The Executor interface provides a single method, execute, designed to be a drop-in replacement for a common thread-creation idiom. Executor接口提供了一个方法execute,它被设计成一个普通线程创建习惯用法的替代方法。If r is a Runnable object, and e is an Executor object you can replace如果r是一个Runnable对象,而e是一个Executor对象,你可以将

(new Thread(r)).start();

with替换为

e.execute(r);

However, the definition of execute is less specific. 但是,execute的定义不太具体。The low-level idiom creates a new thread and launches it immediately. 低级习惯用法创建一个新线程并立即启动它。Depending on the Executor implementation, execute may do the same thing, but is more likely to use an existing worker thread to run r, or to place r in a queue to wait for a worker thread to become available. 根据Executor实现的不同,execute可以执行相同的操作,但更可能使用现有的工作线程来运行r,或者将r放入队列中以等待工作线程可用。(We'll describe worker threads in the section on Thread Pools.)(我们将在线程池一节中描述工作线程。)

The executor implementations in java.util.concurrent are designed to make full use of the more advanced ExecutorService and ScheduledExecutorService interfaces, although they also work with the base Executor interface.java.util.concurrent中的执行器实现旨在充分利用更高级的ExecutorServiceScheduledExecutorService接口,尽管它们也与基本Executor接口一起工作。

The ExecutorService InterfaceExecutorService接口

The ExecutorService interface supplements execute with a similar, but more versatile submit method. ExecutorService接口使用一种类似但更通用的提交方法来补充executeLike execute, submit accepts Runnable objects, but also accepts Callable objects, which allow the task to return a value. execute类似,submit接受Runnable对象,但也接受Callable对象,这允许任务返回值。The submit method returns a Future object, which is used to retrieve the Callable return value and to manage the status of both Callable and Runnable tasks.submit方法返回一个Future对象,该对象用于检索Callable的返回值并管理CallableRunnable任务的状态。

ExecutorService also provides methods for submitting large collections of Callable objects. ExecutorService还提供提交大量Callable对象集合的方法。Finally, ExecutorService provides a number of methods for managing the shutdown of the executor. 最后,ExecutorService提供了许多方法来管理executor的关闭。To support immediate shutdown, tasks should handle interrupts correctly.为了支持立即关机,任务应该正确处理中断

The ScheduledExecutorService InterfaceScheduledExecutorService接口

The ScheduledExecutorService interface supplements the methods of its parent ExecutorService with schedule, which executes a Runnable or Callable task after a specified delay. ScheduledExecutorService接口使用schedule补充其父ExecutorService的方法,schedule在指定延迟后执行RunnableCallable的任务。In addition, the interface defines scheduleAtFixedRate and scheduleWithFixedDelay, which executes specified tasks repeatedly, at defined intervals.此外,该接口定义scheduleAtFixedRatescheduleWithFixedDelay,它们以定义的间隔重复执行指定的任务。


Previous page: Executors
Next page: Thread Pools