| Executor.java |
1 /*
2 * %W% %E%
3 *
4 * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
5 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6 */
7
8 package java.util.concurrent;
9
10 /**
11 * An object that executes submitted {@link Runnable} tasks. This
12 * interface provides a way of decoupling task submission from the
13 * mechanics of how each task will be run, including details of thread
14 * use, scheduling, etc. An <tt>Executor</tt> is normally used
15 * instead of explicitly creating threads. For example, rather than
16 * invoking <tt>new Thread(new(RunnableTask())).start()</tt> for each
17 * of a set of tasks, you might use:
18 *
19 * <pre>
20 * Executor executor = <em>anExecutor</em>;
21 * executor.execute(new RunnableTask1());
22 * executor.execute(new RunnableTask2());
23 * ...
24 * </pre>
25 *
26 * However, the <tt>Executor</tt> interface does not strictly
27 * require that execution be asynchronous. In the simplest case, an
28 * executor can run the submitted task immediately in the caller's
29 * thread:
30 *
31 * <pre>
32 * class DirectExecutor implements Executor {
33 * public void execute(Runnable r) {
34 * r.run();
35 * }
36 * }</pre>
37 *
38 * More typically, tasks are executed in some thread other
39 * than the caller's thread. The executor below spawns a new thread
40 * for each task.
41 *
42 * <pre>
43 * class ThreadPerTaskExecutor implements Executor {
44 * public void execute(Runnable r) {
45 * new Thread(r).start();
46 * }
47 * }</pre>
48 *
49 * Many <tt>Executor</tt> implementations impose some sort of
50 * limitation on how and when tasks are scheduled. The executor below
51 * serializes the submission of tasks to a second executor,
52 * illustrating a composite executor.
53 *
54 * <pre>
55 * class SerialExecutor implements Executor {
56 * final Queue<Runnable> tasks = new ArrayDeque<Runnable>();
57 * final Executor executor;
58 * Runnable active;
59 *
60 * SerialExecutor(Executor executor) {
61 * this.executor = executor;
62 * }
63 *
64 * public synchronized void execute(final Runnable r) {
65 * tasks.offer(new Runnable() {
66 * public void run() {
67 * try {
68 * r.run();
69 * } finally {
70 * scheduleNext();
71 * }
72 * }
73 * });
74 * if (active == null) {
75 * scheduleNext();
76 * }
77 * }
78 *
79 * protected synchronized void scheduleNext() {
80 * if ((active = tasks.poll()) != null) {
81 * executor.execute(active);
82 * }
83 * }
84 * }</pre>
85 *
86 * The <tt>Executor</tt> implementations provided in this package
87 * implement {@link ExecutorService}, which is a more extensive
88 * interface. The {@link ThreadPoolExecutor} class provides an
89 * extensible thread pool implementation. The {@link Executors} class
90 * provides convenient factory methods for these Executors.
91 *
92 * <p>Memory consistency effects: Actions in a thread prior to
93 * submitting a {@code Runnable} object to an {@code Executor}
94 * <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a>
95 * its execution begins, perhaps in another thread.
96 *
97 * @since 1.5
98 * @author Doug Lea
99 */
100 public interface Executor {
101
102 /**
103 * Executes the given command at some time in the future. The command
104 * may execute in a new thread, in a pooled thread, or in the calling
105 * thread, at the discretion of the <tt>Executor</tt> implementation.
106 *
107 * @param command the runnable task
108 * @throws RejectedExecutionException if this task cannot be
109 * accepted for execution.
110 * @throws NullPointerException if command is null
111 */
112 void execute(Runnable command);
113 }
114