ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/ExecutorService.java
Revision: 1.41
Committed: Fri Sep 2 01:10:52 2005 UTC (18 years, 9 months ago) by brian
Branch: MAIN
Changes since 1.40: +1 -1 lines
Log Message:
Formatting tweaks to happens-before markup

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 import java.util.concurrent.*; // for javadoc (till 6280605 is fixed)
9 import java.util.List;
10 import java.util.Collection;
11 import java.security.PrivilegedAction;
12 import java.security.PrivilegedExceptionAction;
13
14 /**
15 * An {@link Executor} that provides methods to manage termination and
16 * methods that can produce a {@link Future} for tracking progress of
17 * one or more asynchronous tasks.
18 *
19 * <p>
20 * An <tt>ExecutorService</tt> can be shut down, which will cause it
21 * to stop accepting new tasks. After being shut down, the executor
22 * will eventually terminate, at which point no tasks are actively
23 * executing, no tasks are awaiting execution, and no new tasks can be
24 * submitted.
25 *
26 * <p> Method <tt>submit</tt> extends base method {@link
27 * Executor#execute} by creating and returning a {@link Future} that
28 * can be used to cancel execution and/or wait for completion.
29 * Methods <tt>invokeAny</tt> and <tt>invokeAll</tt> perform the most
30 * commonly useful forms of bulk execution, executing a collection of
31 * tasks and then waiting for at least one, or all, to
32 * complete. (Class {@link ExecutorCompletionService} can be used to
33 * write customized variants of these methods.)
34 *
35 * <p>The {@link Executors} class provides factory methods for the
36 * executor services provided in this package.
37 *
38 * <h3>Usage Example</h3>
39 *
40 * Here is a sketch of a network service in which threads in a thread
41 * pool service incoming requests. It uses the preconfigured {@link
42 * Executors#newFixedThreadPool} factory method:
43 *
44 * <pre>
45 * class NetworkService {
46 * private final ServerSocket serverSocket;
47 * private final ExecutorService pool;
48 *
49 * public NetworkService(int port, int poolSize)
50 * throws IOException {
51 * serverSocket = new ServerSocket(port);
52 * pool = Executors.newFixedThreadPool(poolSize);
53 * }
54 *
55 * public void serve() {
56 * try {
57 * for (;;) {
58 * pool.execute(new Handler(serverSocket.accept()));
59 * }
60 * } catch (IOException ex) {
61 * pool.shutdown();
62 * }
63 * }
64 * }
65 *
66 * class Handler implements Runnable {
67 * private final Socket socket;
68 * Handler(Socket socket) { this.socket = socket; }
69 * public void run() {
70 * // read and service request
71 * }
72 * }
73 * </pre>
74 *
75 * Memory visibility effects: State changes to a <tt>Runnable</tt> or
76 * <tt>Callable</tt> object made prior to submission to an
77 * <tt>Executor</tt> <a
78 * href="package-summary.html#MemoryVisibility"><i>happen-before</i></a>
79 * its execution, and state changes to objects returned from a
80 * <tt>Callable</tt> <i>happen-before</i> that object is returned to
81 * a thread which calls <tt>Future.get()</tt> to retrieve that result.
82 *
83 * @since 1.5
84 * @author Doug Lea
85 */
86 public interface ExecutorService extends Executor {
87
88 /**
89 * Initiates an orderly shutdown in which previously submitted
90 * tasks are executed, but no new tasks will be accepted.
91 * Invocation has no additional effect if already shut down.
92 *
93 * @throws SecurityException if a security manager exists and
94 * shutting down this ExecutorService may manipulate
95 * threads that the caller is not permitted to modify
96 * because it does not hold {@link
97 * java.lang.RuntimePermission}<tt>("modifyThread")</tt>,
98 * or the security manager's <tt>checkAccess</tt> method
99 * denies access.
100 */
101 void shutdown();
102
103 /**
104 * Attempts to stop all actively executing tasks, halts the
105 * processing of waiting tasks, and returns a list of the tasks that were
106 * awaiting execution.
107 *
108 * <p>There are no guarantees beyond best-effort attempts to stop
109 * processing actively executing tasks. For example, typical
110 * implementations will cancel via {@link Thread#interrupt}, so any
111 * task that fails to respond to interrupts may never terminate.
112 *
113 * @return list of tasks that never commenced execution
114 * @throws SecurityException if a security manager exists and
115 * shutting down this ExecutorService may manipulate
116 * threads that the caller is not permitted to modify
117 * because it does not hold {@link
118 * java.lang.RuntimePermission}<tt>("modifyThread")</tt>,
119 * or the security manager's <tt>checkAccess</tt> method
120 * denies access.
121 */
122 List<Runnable> shutdownNow();
123
124 /**
125 * Returns <tt>true</tt> if this executor has been shut down.
126 *
127 * @return <tt>true</tt> if this executor has been shut down
128 */
129 boolean isShutdown();
130
131 /**
132 * Returns <tt>true</tt> if all tasks have completed following shut down.
133 * Note that <tt>isTerminated</tt> is never <tt>true</tt> unless
134 * either <tt>shutdown</tt> or <tt>shutdownNow</tt> was called first.
135 *
136 * @return <tt>true</tt> if all tasks have completed following shut down
137 */
138 boolean isTerminated();
139
140 /**
141 * Blocks until all tasks have completed execution after a shutdown
142 * request, or the timeout occurs, or the current thread is
143 * interrupted, whichever happens first.
144 *
145 * @param timeout the maximum time to wait
146 * @param unit the time unit of the timeout argument
147 * @return <tt>true</tt> if this executor terminated and
148 * <tt>false</tt> if the timeout elapsed before termination
149 * @throws InterruptedException if interrupted while waiting
150 */
151 boolean awaitTermination(long timeout, TimeUnit unit)
152 throws InterruptedException;
153
154
155 /**
156 * Submits a value-returning task for execution and returns a
157 * Future representing the pending results of the task. The
158 * Future's <tt>get</tt> method will return the task's result upon
159 * successful completion.
160 *
161 * <p>
162 * If you would like to immediately block waiting
163 * for a task, you can use constructions of the form
164 * <tt>result = exec.submit(aCallable).get();</tt>
165 *
166 * <p> Note: The {@link Executors} class includes a set of methods
167 * that can convert some other common closure-like objects,
168 * for example, {@link java.security.PrivilegedAction} to
169 * {@link Callable} form so they can be submitted.
170 *
171 * @param task the task to submit
172 * @return a Future representing pending completion of the task
173 * @throws RejectedExecutionException if the task cannot be
174 * scheduled for execution
175 * @throws NullPointerException if the task is null
176 */
177 <T> Future<T> submit(Callable<T> task);
178
179 /**
180 * Submits a Runnable task for execution and returns a Future
181 * representing that task. The Future's <tt>get</tt> method will
182 * return the given result upon successful completion.
183 *
184 * @param task the task to submit
185 * @param result the result to return
186 * @return a Future representing pending completion of the task
187 * @throws RejectedExecutionException if the task cannot be
188 * scheduled for execution
189 * @throws NullPointerException if the task is null
190 */
191 <T> Future<T> submit(Runnable task, T result);
192
193 /**
194 * Submits a Runnable task for execution and returns a Future
195 * representing that task. The Future's <tt>get</tt> method will
196 * return <tt>null</tt> upon <em>successful</em> completion.
197 *
198 * @param task the task to submit
199 * @return a Future representing pending completion of the task
200 * @throws RejectedExecutionException if the task cannot be
201 * scheduled for execution
202 * @throws NullPointerException if the task is null
203 */
204 Future<?> submit(Runnable task);
205
206 /**
207 * Executes the given tasks, returning a list of Futures holding
208 * their status and results when all complete.
209 * {@link Future#isDone} is <tt>true</tt> for each
210 * element of the returned list.
211 * Note that a <em>completed</em> task could have
212 * terminated either normally or by throwing an exception.
213 * The results of this method are undefined if the given
214 * collection is modified while this operation is in progress.
215 *
216 * @param tasks the collection of tasks
217 * @return A list of Futures representing the tasks, in the same
218 * sequential order as produced by the iterator for the
219 * given task list, each of which has completed.
220 * @throws InterruptedException if interrupted while waiting, in
221 * which case unfinished tasks are cancelled.
222 * @throws NullPointerException if tasks or any of its elements are <tt>null</tt>
223 * @throws RejectedExecutionException if any task cannot be
224 * scheduled for execution
225 */
226
227 <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
228 throws InterruptedException;
229
230 /**
231 * Executes the given tasks, returning a list of Futures holding
232 * their status and results
233 * when all complete or the timeout expires, whichever happens first.
234 * {@link Future#isDone} is <tt>true</tt> for each
235 * element of the returned list.
236 * Upon return, tasks that have not completed are cancelled.
237 * Note that a <em>completed</em> task could have
238 * terminated either normally or by throwing an exception.
239 * The results of this method are undefined if the given
240 * collection is modified while this operation is in progress.
241 *
242 * @param tasks the collection of tasks
243 * @param timeout the maximum time to wait
244 * @param unit the time unit of the timeout argument
245 * @return a list of Futures representing the tasks, in the same
246 * sequential order as produced by the iterator for the
247 * given task list. If the operation did not time out,
248 * each task will have completed. If it did time out, some
249 * of these tasks will not have completed.
250 * @throws InterruptedException if interrupted while waiting, in
251 * which case unfinished tasks are cancelled
252 * @throws NullPointerException if tasks, any of its elements, or
253 * unit are <tt>null</tt>
254 * @throws RejectedExecutionException if any task cannot be scheduled
255 * for execution
256 */
257 <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks,
258 long timeout, TimeUnit unit)
259 throws InterruptedException;
260
261 /**
262 * Executes the given tasks, returning the result
263 * of one that has completed successfully (i.e., without throwing
264 * an exception), if any do. Upon normal or exceptional return,
265 * tasks that have not completed are cancelled.
266 * The results of this method are undefined if the given
267 * collection is modified while this operation is in progress.
268 *
269 * @param tasks the collection of tasks
270 * @return the result returned by one of the tasks
271 * @throws InterruptedException if interrupted while waiting
272 * @throws NullPointerException if tasks or any of its elements
273 * are <tt>null</tt>
274 * @throws IllegalArgumentException if tasks is empty
275 * @throws ExecutionException if no task successfully completes
276 * @throws RejectedExecutionException if tasks cannot be scheduled
277 * for execution
278 */
279 <T> T invokeAny(Collection<? extends Callable<T>> tasks)
280 throws InterruptedException, ExecutionException;
281
282 /**
283 * Executes the given tasks, returning the result
284 * of one that has completed successfully (i.e., without throwing
285 * an exception), if any do before the given timeout elapses.
286 * Upon normal or exceptional return, tasks that have not
287 * completed are cancelled.
288 * The results of this method are undefined if the given
289 * collection is modified while this operation is in progress.
290 *
291 * @param tasks the collection of tasks
292 * @param timeout the maximum time to wait
293 * @param unit the time unit of the timeout argument
294 * @return the result returned by one of the tasks.
295 * @throws InterruptedException if interrupted while waiting
296 * @throws NullPointerException if tasks, any of its elements, or
297 * unit are <tt>null</tt>
298 * @throws TimeoutException if the given timeout elapses before
299 * any task successfully completes
300 * @throws ExecutionException if no task successfully completes
301 * @throws RejectedExecutionException if tasks cannot be scheduled
302 * for execution
303 */
304 <T> T invokeAny(Collection<? extends Callable<T>> tasks,
305 long timeout, TimeUnit unit)
306 throws InterruptedException, ExecutionException, TimeoutException;
307 }