Executor Interfaces <译>
原文:https://docs.oracle.com/javase/tutorial/essential/concurrency/exinter.html
The java.util.concurrent package defines three executor interfaces:
java.util.concurrent package
定义了三种executor
接口:
- Executor, a simple interface that supports launching new tasks.
Executor, 一个支持启动新任务的简单接口。 - ExecutorService, a subinterface of Executor, which adds features that help manage the life cycle, both of the individual tasks and of the executor itself.
ExecutorService,Executor的子接口,它添加了有助于管理生命周期的功能,包含了单个任务以及执行程序本身。 - ScheduledExecutorService, a subinterface of ExecutorService, supports future and/or periodic execution of tasks.
ScheduledExecutorService, ExecutorService的子接口,支持延时执行and/or
定期执行任务的功能。
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 Interface
The Executor interface provides a single method, execute, designed to be a drop-in replacement for a common thread-creation idiom. If r is a Runnable object, and e is an Executor object you can replace
(new Thread(r)).start();
with
e.execute(r);
Executor接口提供了一个单独的方法execute
,旨在作为常见线程创建习惯用法的直接替代品。如果r
是一个Runnable对象,并且e
是一个Executor对象,则你可以用
e.execute(r);
取代(new Thread(r)).start();
However, the definition of execute is less specific. 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. (We’ll describe worker threads in the section on Thread Pools.)
但是,执行的定义不太具体。低级习惯创建一个新线程并立即启动它。根据Executor
的实现,execute
可能会执行相同的操作,但更有可能使用现存的工作线程来运行r
,或者将r
放入队列中以等待工作线程可用(我们将会在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
中的executor实现被设计为能充分利用更多高级特性的ExecutorService
和ScheduledExecutorService
接口,尽管它们也适用于基本的Executor接口。
The ExecutorService Interface
The ExecutorService interface supplements execute with a similar, but more versatile submit method. Like execute, submit accepts Runnable objects, but also accepts Callable objects, which allow the task to return a value. 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.
ExecutorService 接口补充execute
使用类似但更通用的提交方法。可以跟execute
一样提交接收Runnable
对象,也可以接受一个Callable
对象,它允许任务返回一个值。这个提交方法返回一个Future
对象,它被用来检索Callable
对象的返回值并管理 Callable
对象和Runable
对像任务的状态。
ExecutorService also provides methods for submitting large collections of Callable objects. Finally, ExecutorService provides a number of methods for managing the shutdown of the executor. To support immediate shutdown, tasks should handle interrupts correctly.
ExecutorService还提供了提交大型Callable
对像集合的方法。最后,ExecutorService 提供了许多方法来管理执行程序的关闭。若要支持立即关闭,任务应正确处理中断。
The ScheduledExecutorService Interface
The ScheduledExecutorService interface supplements the methods of its parent ExecutorService with schedule, which executes a Runnable or Callable task after a specified delay. In addition, the interface defines scheduleAtFixedRate and scheduleWithFixedDelay, which executes specified tasks repeatedly, at defined intervals.
ScheduledExecutorService 接口用 schedule 补充其父 ExecutorService 的方法,该 schedule 在指定的延迟后执行 Runnable 或 Callable 任务。此外,该接口还定义了 scheduleAtFixedRate 和 scheduleWithFixedDelay方法,它们以定义的时间间隔重复执行指定的任务。
评论