Executor
All Known Subinterfaces:
ExecutorService, ScheduledExecutorService
All Known Implementing Classes:
AbstractExecutorService, ForkJoinPool, ScheduledThreadPoolExecutor, ThreadPoolExecutor
public interface Executor An object that executes submitted Runnable tasks. This interface provides a way of decoupling task submission from the mechanics of how each task will be run(该界面提供了一种将任务提交与每个任务如何运行的机制解耦的方法), including details of thread use, scheduling(调度), etc(等等). An Executor is normally used instead(代替) of explicitly(明确) creating threads. For example, rather than(而不是) invoking new Thread(new(RunnableTask())).start() for each of a set of tasks(而不是为一组任务调用新的), you might use:
Executor executor = anExecutor;
executor.execute(new RunnableTask1());
executor.execute(new RunnableTask2());
...
【Executor】However, the Executor interface does not strictly(严格) require(要求) that execution be asynchronous(异步). In the simplest(简单) case, an executor can run the submitted task immediately(立即) in the caller’s thread:
class DirectExecutor implements Executor {
public void execute(Runnable r) {
r.run();
}
}
More typically, tasks are executed in some thread other than the caller’s thread. The executor below(下面) spawns(大量生产) a new thread for each task.
class ThreadPerTaskExecutor implements Executor {
public void execute(Runnable r) {
new Thread(r).start();
}
}
Many Executor implementations impose(强加) some sort(某些,某种) of limitation(限制) on how and when tasks are scheduled(任务如何和何时安排). The executor below serializes(连续) the submission of tasks to a second executor, illustrating(说明) a composite(合成) executor(说明复合执行器。).
class SerialExecutor implements Executor {
final Queue tasks = new ArrayDeque();
final Executor executor;
Runnable active;
SerialExecutor(Executor executor) {
this.executor = executor;
}public synchronized void execute(final Runnable r) {
tasks.offer(new Runnable() {
public void run() {
try {
r.run();
} finally {
scheduleNext();
}
}
});
if (active == null) {
scheduleNext();
}
}protected synchronized void scheduleNext() {
if ((active = tasks.poll()) != null) {
executor.execute(active);
}
}
}
The Executor implementations provided in this package implement ExecutorService, which is a more extensive interface(这是一个更广泛的Interface). The ThreadPoolExecutor class provides an extensible(扩展) thread pool implementation. The Executors class provides convenient(方便) factory methods for these Executors.
Memory consistency(一致性) effects(效应): Actions in a thread prior(预先) to submitting a Runnable object to an Executor happen-before(发生-前) its execution begins, perhaps(或者) in another thread.
Since:
1.5
Method Detail execute
void execute(Runnable command)
Executes the given command at some time in the future. The command may execute in a new thread, in a pooled thread, or in the calling thread, at the discretion of the Executor implementation.
Parameters:
command - the runnable task
Throws:
RejectedExecutionException - if this task cannot be accepted for execution
NullPointerException - if command is null
推荐阅读
- parallels|parallels desktop 解决网络初始化失败问题
- Android|Android install 多个设备时指定设备
- oracle|oracle java jdk install
- JavaScript|JavaScript — call()和apply()、Date对象、Math、包装类、字符串的方法
- c语言|一文搞懂栈(stack)、堆(heap)、单片机裸机内存管理malloc
- 2018-11-13每日一词|2018-11-13每日一词 42 | parallel - 草稿
- 201708期+283号+薛泽明Allen+Day34
- pyinstaller解决闪退问题
- Python|Python TypeError: 'module' object is not callable 原因分析
- 并发,并行,阻塞,非阻塞,异步,同步