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包定义了三个执行器接口:
ExecutorExecutorServiceExecutor, which adds features that help manage the life cycle, both of the individual tasks and of the executor itself.Executor的子接口,它添加了有助于管理生命周期的功能,包括单个任务和Executor本身。ScheduledExecutorServiceExecutorService, 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,它们以定义的间隔重复执行指定的任务。