The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
The java.util.concurrent
package defines three executor interfaces:java.util.concurrent
包定义了三个执行器接口:
Executor
ExecutorService
Executor
, which adds features that help manage the life cycle, both of the individual tasks and of the executor itself.Executor
的子接口,它添加了有助于管理生命周期的功能,包括单个任务和Executor
本身。ScheduledExecutorService
ExecutorService
, supports future and/or periodic execution of tasks.ExecutorService
的子接口,支持将来和/或定期执行任务。Typically, variables that refer to executor objects are declared as one of these three interface types, not with an executor class type.通常,引用executor
对象的变量被声明为这三种接口类型之一,而不是executor
类类型。
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 replacer
是一个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
中的执行器实现旨在充分利用更高级的ExecutorService
和ScheduledExecutorService
接口,尽管它们也与基本Executor
接口一起工作。
ExecutorService
InterfaceExecutorService
接口The ExecutorService
interface supplements execute
with a similar, but more versatile submit
method. ExecutorService
接口使用一种类似但更通用的提交方法来补充execute
。Like 与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
的返回值并管理Callable
和Runnable
任务的状态。
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.为了支持立即关机,任务应该正确处理中断。
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
在指定延迟后执行Runnable
或Callable
的任务。In addition, the interface defines 此外,该接口定义scheduleAtFixedRate
and scheduleWithFixedDelay
, which executes specified tasks repeatedly, at defined intervals.scheduleAtFixedRate
和scheduleWithFixedDelay
,它们以定义的间隔重复执行指定的任务。