ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/Executor.java
Revision: 1.15
Committed: Sat Dec 27 19:26:25 2003 UTC (20 years, 5 months ago) by dl
Branch: MAIN
Changes since 1.14: +2 -2 lines
Log Message:
Headers reference Creative Commons

File Contents

# Content
1 /*
2 * Written by Doug Lea with assistance from members of JCP JSR-166
3 * Expert Group and released to the public domain, as explained at
4 * http://creativecommons.org/licenses/publicdomain
5 */
6
7 package java.util.concurrent;
8
9 /**
10 * An object that executes submitted {@link Runnable} tasks. This
11 * interface provides a way of decoupling task submission from the
12 * mechanics of how each task will be run, including details of thread
13 * use, scheduling, etc. In the simplest case, an executor can run
14 * the submitted task immediately in the caller's thread:
15 *
16 * <pre>
17 * class DirectExecutor implements Executor {
18 * public void execute(Runnable r) {
19 * r.run();
20 * }
21 * }</pre>
22 *
23 * However, tasks are typically executed in a different thread than
24 * the caller's thread. The executor below spawns a new thread for
25 * each task.
26 *
27 * <pre>
28 * class ThreadPerTaskExecutor implements Executor {
29 * public void execute(Runnable r) {
30 * new Thread(r).start();
31 * }
32 * }</pre>
33 *
34 * Most <tt>Executor</tt> implementations will impose some sort of
35 * limitation on how and when tasks are scheduled. The executor below
36 * serializes the submission of tasks to a second executor,
37 * illustrating a composite executor.
38 *
39 * <pre>
40 * class SerialExecutor implements Executor {
41 * final Queue&lt;Runnable&gt; tasks = new LinkedBlockingQueue&lt;Runnable&gt;();
42 * final Executor executor;
43 * Runnable active;
44 *
45 * SerialExecutor(Executor executor) {
46 * this.executor = executor;
47 * }
48 *
49 * public synchronized void execute(final Runnable r) {
50 * tasks.offer(new Runnable() {
51 * public void run() {
52 * try {
53 * r.run();
54 * } finally {
55 * scheduleNext();
56 * }
57 * }
58 * });
59 * if (active == null) {
60 * scheduleNext();
61 * }
62 * }
63 *
64 * protected synchronized void scheduleNext() {
65 * if ((active = tasks.poll()) != null) {
66 * executor.execute(active);
67 * }
68 * }
69 * }</pre>
70 *
71 * The <tt>Executor</tt> implementations provided in this package
72 * implement {@link ExecutorService}, which is a more extensive
73 * interface. The {@link ThreadPoolExecutor} class provides an
74 * extensible thread pool implementation. The {@link Executors} class
75 * provides convenient factory methods for these Executors.
76 *
77 * @since 1.5
78 * @author Doug Lea
79 */
80 public interface Executor {
81
82 /**
83 * Executes the given command at some time in the future. The command
84 * may execute in a new thread, in a pooled thread, or in the calling
85 * thread, at the discretion of the <tt>Executor</tt> implementation.
86 *
87 * @param command the runnable task
88 * @throws RejectedExecutionException if this task cannot be
89 * accepted for execution.
90 * @throws NullPointerException if command is null
91 */
92 void execute(Runnable command);
93 }