ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/Executor.java
Revision: 1.34
Committed: Mon Feb 16 17:43:05 2015 UTC (9 years, 3 months ago) by jsr166
Branch: MAIN
Changes since 1.33: +1 -1 lines
Log Message:
Fix for https://bugs.openjdk.java.net/browse/JDK-8073208 javadoc typo in java.util.concurrent.Executor

File Contents

# User Rev Content
1 tim 1.1 /*
2 dl 1.2 * Written by Doug Lea with assistance from members of JCP JSR-166
3 dl 1.15 * Expert Group and released to the public domain, as explained at
4 jsr166 1.28 * http://creativecommons.org/publicdomain/zero/1.0/
5 tim 1.1 */
6    
7     package java.util.concurrent;
8    
9     /**
10 dl 1.11 * 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 jsr166 1.30 * use, scheduling, etc. An {@code Executor} is normally used
14 dl 1.16 * instead of explicitly creating threads. For example, rather than
15 jsr166 1.34 * invoking {@code new Thread(new RunnableTask()).start()} for each
16 dl 1.16 * of a set of tasks, you might use:
17     *
18     * <pre>
19     * Executor executor = <em>anExecutor</em>;
20     * executor.execute(new RunnableTask1());
21     * executor.execute(new RunnableTask2());
22     * ...
23     * </pre>
24 jsr166 1.19 *
25 jsr166 1.30 * However, the {@code Executor} interface does not strictly
26 dl 1.16 * require that execution be asynchronous. In the simplest case, an
27     * executor can run the submitted task immediately in the caller's
28     * thread:
29 tim 1.1 *
30 jsr166 1.29 * <pre> {@code
31 tim 1.1 * class DirectExecutor implements Executor {
32 jsr166 1.29 * public void execute(Runnable r) {
33     * r.run();
34     * }
35     * }}</pre>
36 tim 1.1 *
37 dl 1.18 * More typically, tasks are executed in some thread other
38 dl 1.16 * than the caller's thread. The executor below spawns a new thread
39     * for each task.
40 tim 1.1 *
41 jsr166 1.29 * <pre> {@code
42 tim 1.1 * class ThreadPerTaskExecutor implements Executor {
43 jsr166 1.29 * public void execute(Runnable r) {
44     * new Thread(r).start();
45     * }
46     * }}</pre>
47 tim 1.1 *
48 jsr166 1.30 * Many {@code Executor} implementations impose some sort of
49 dl 1.10 * limitation on how and when tasks are scheduled. The executor below
50     * serializes the submission of tasks to a second executor,
51     * illustrating a composite executor.
52 tim 1.1 *
53 jsr166 1.27 * <pre> {@code
54 tim 1.1 * class SerialExecutor implements Executor {
55 jsr166 1.33 * final Queue<Runnable> tasks = new ArrayDeque<>();
56 jsr166 1.27 * final Executor executor;
57     * Runnable active;
58     *
59     * SerialExecutor(Executor executor) {
60     * this.executor = executor;
61     * }
62     *
63     * public synchronized void execute(final Runnable r) {
64 jsr166 1.32 * tasks.add(new Runnable() {
65 jsr166 1.27 * public void run() {
66     * try {
67     * r.run();
68     * } finally {
69     * scheduleNext();
70 tim 1.1 * }
71 jsr166 1.27 * }
72     * });
73     * if (active == null) {
74     * scheduleNext();
75 tim 1.1 * }
76 jsr166 1.27 * }
77 tim 1.1 *
78 jsr166 1.27 * protected synchronized void scheduleNext() {
79     * if ((active = tasks.poll()) != null) {
80     * executor.execute(active);
81 tim 1.1 * }
82 jsr166 1.27 * }
83     * }}</pre>
84 tim 1.1 *
85 jsr166 1.30 * The {@code Executor} implementations provided in this package
86 dl 1.10 * implement {@link ExecutorService}, which is a more extensive
87     * interface. The {@link ThreadPoolExecutor} class provides an
88     * extensible thread pool implementation. The {@link Executors} class
89     * provides convenient factory methods for these Executors.
90 tim 1.1 *
91 jsr166 1.25 * <p>Memory consistency effects: Actions in a thread prior to
92     * submitting a {@code Runnable} object to an {@code Executor}
93     * <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a>
94     * its execution begins, perhaps in another thread.
95 brian 1.21 *
96 tim 1.1 * @since 1.5
97 dl 1.7 * @author Doug Lea
98 tim 1.1 */
99     public interface Executor {
100    
101     /**
102     * Executes the given command at some time in the future. The command
103     * may execute in a new thread, in a pooled thread, or in the calling
104 jsr166 1.30 * thread, at the discretion of the {@code Executor} implementation.
105 tim 1.1 *
106 jozart 1.6 * @param command the runnable task
107 dl 1.10 * @throws RejectedExecutionException if this task cannot be
108 jsr166 1.31 * accepted for execution
109 dl 1.12 * @throws NullPointerException if command is null
110 tim 1.1 */
111 jozart 1.6 void execute(Runnable command);
112 tim 1.1 }