Executors
In all of the previous examples, there’s a close connection between the task being done by a new thread, as defined by its Runnable object, and the thread itself, as defined by aThread object. This works well for small applications, but in large-scale applications, it makes sense to separate thread management and creation from the rest of the application. Objects that encapsulate these functions are known as executors. The following subsections describe executors in detail.
Executor Interfaces define the three executor object types.
Thread Pools are the most common kind of executor implementation.
Fork/Join is a framework (new in JDK 7) for taking advantage of multiple processors.
译文:
执行器
在所有以前的实例中,我们对任务使用新线程有一个近距离的接触。作为Runnable对象定义的线程,和线程,通过一个Thread对象定义的。这对小型程序工作的很好,但是对大型程序,对于线程与其他程序分开管理是有必要的。封转了这些函数的对象是executors.接下来的章节详细的描述了executors。
执行接口 定义了三种执行对象类型
线程池 是最常见的执行实现。
分开与合并是一个框架(在JDK7新加的)对于多处理器非常有用。
Executor Interfaces
The java.util.concurrent package defines three executor interfaces:
Executor, a simple interface that supports launching new tasks.
ExecutorService, a subinterface of Executor, which adds features that help manage the lifecycle, both of the individual tasks and of the executor itself.
ScheduledExecutorService, a subinterface of ExecutorService, supports future and/or periodic execution of tasks.
Typically, variables that refer to executor objects are declared as one of these three interface types, not with an executor class type.
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);
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.)
The executor implementations in java.util.concurrent are designed to make full use of the more advanced ExecutorService andScheduledExecutorService interfaces, although they also work with the base Executor interface.
The ExecutorService Interface
The ExecutorService interface supplements execute with a similar, but more versatile submit method. Like execute, submitaccepts 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 andRunnable tasks.
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 handleinterrupts correctly.
The ScheduledExecutorService Interface
The ScheduledExecutorService interface supplements the methods of its parent ExecutorService with schedule, which executes aRunnable or Callable task after a specified delay. In addition, the interface defines scheduleAtFixedRate andscheduleWithFixedDelay, which executes specified tasks repeatedly, at defined intervals.
译文:
执行接口
java.util.concurrent包定义了三种执行接口:
执行器,一种简单的接口支持载入新任务。
执行服务,执行器的一个子接口,它增加了管理生命周期的特性,包括私有的任务和执行器本身。
调度执行服务,执行服务的一个子接口,支持将来或者周期执行的任务。
代表性的,可执行的对象变量被定义为这三种类型的一种,而不是一个可执行类的类型。执行器接口Executor接口提供了一个简单的方法,执行,定义了一种通用的创建线程的规则。如果r是一个Runnable对象,e是一个执行器对象,你可使用如下的方法替代。
(new Thread(r)).start();
替代为:e.execute(r);
然而,execute的定义是没有那么严格的。低级的创建线程的规则是创建之后马上载入它。依赖于实现Executor接口,execute可以实现相同的事情。但是跟喜欢用一个已经催在的工作线程来运行它,或者在一个队列中替代r来等待工作线程变成可用的。(我们将会在线程池这一节描述工作线程)
在java.util.concurrent中是想的执行器被定义为更加充分的使用ExecutorService和ScheduleExecutorService接口。尽管他们都是基于Executor接口的。
执行服务接口
执行服务接口补充了执行器,但是更多的通用的提交方法。像执行器,提交方法接受了Runable对象,但是也接受额Callable对象,它语序任务返回一个值。submit方法返回一个Future对象,它被用来重新检索Callable返回值和管理Callable和Runnable任务的状态。
执行服务也提供了一种方法提交Callable对象的大型集合。ExecutorService提供了许多方法管理关闭执行器。为了支持立即提交,任务需要实现正确实现interrupts方法。
调度执行服务接口
调度服务接口提供了基于它的父类执行服务的许多调度方法,可以执行Runnable或者Callable任务在指定的一个延迟之后。进一步,这个接口定义了schedulAtFixedRate和scheduleWithFixDelay,支持重复执行指定的任务,在一个定义的间隔内。