ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/ExecutorService.java
Revision: 1.17
Committed: Fri Dec 19 14:42:25 2003 UTC (20 years, 5 months ago) by dl
Branch: MAIN
Changes since 1.16: +111 -96 lines
Log Message:
Documentation improvements

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. Use, modify, and
4 * redistribute this code in any way without acknowledgement.
5 */
6
7 package java.util.concurrent;
8
9 import java.util.List;
10 import java.util.Collection;
11 import java.security.PrivilegedAction;
12 import java.security.PrivilegedExceptionAction;
13
14 /**
15 * An <tt>Executor</tt> that provides methods to manage termination
16 * and methods that can produce a {@link Future} for tracking
17 * progress of one or more asynchronous tasks.
18 * <p>
19 *
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> and related methods extend base method
27 * <tt>execute</tt> 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 complete. (Class
32 * {@link ExecutorCompletionService} can be used to write customizable
33 * versions of such methods.)
34 *
35 * <p>The {@link Executors} class provides factory methods for the
36 * executor services provided in this package.
37 *
38 * @since 1.5
39 * @author Doug Lea
40 */
41 public interface ExecutorService extends Executor {
42
43
44 /**
45 * Initiates an orderly shutdown in which previously submitted
46 * tasks are executed, but no new tasks will be
47 * accepted. Invocation has no additional effect if already shut
48 * down.
49 * @throws SecurityException if a security manager exists and
50 * shutting down this ExecutorService may manipulate threads that
51 * the caller is not permitted to modify because it does not hold
52 * {@link java.lang.RuntimePermission}<tt>("modifyThread")</tt>,
53 * or the security manager's <tt>checkAccess</tt> method denies access.
54 */
55 void shutdown();
56
57 /**
58 * Attempts to stop all actively executing tasks, halts the
59 * processing of waiting tasks, and returns a list of the tasks that were
60 * awaiting execution.
61 *
62 * <p>There are no guarantees beyond best-effort attempts to stop
63 * processing actively executing tasks. For example, typical
64 * implementations will cancel via {@link Thread#interrupt}, so if any
65 * tasks mask or fail to respond to interrupts, they may never terminate.
66 *
67 * @return list of tasks that never commenced execution
68 * @throws SecurityException if a security manager exists and
69 * shutting down this ExecutorService may manipulate threads that
70 * the caller is not permitted to modify because it does not hold
71 * {@link java.lang.RuntimePermission}<tt>("modifyThread")</tt>,
72 * or the security manager's <tt>checkAccess</tt> method denies access.
73 */
74 List<Runnable> shutdownNow();
75
76 /**
77 * Returns <tt>true</tt> if this executor has been shut down.
78 *
79 * @return <tt>true</tt> if this executor has been shut down
80 */
81 boolean isShutdown();
82
83 /**
84 * Returns <tt>true</tt> if all tasks have completed following shut down.
85 * Note that <tt>isTerminated</tt> is never <tt>true</tt> unless
86 * either <tt>shutdown</tt> or <tt>shutdownNow</tt> was called first.
87 *
88 * @return <tt>true</tt> if all tasks have completed following shut down
89 */
90 boolean isTerminated();
91
92 /**
93 * Blocks until all tasks have completed execution after a shutdown
94 * request, or the timeout occurs, or the current thread is
95 * interrupted, whichever happens first.
96 *
97 * @param timeout the maximum time to wait
98 * @param unit the time unit of the timeout argument
99 * @return <tt>true</tt> if this executor terminated and <tt>false</tt>
100 * if the timeout elapsed before termination
101 * @throws InterruptedException if interrupted while waiting
102 */
103 boolean awaitTermination(long timeout, TimeUnit unit)
104 throws InterruptedException;
105
106
107 /**
108 * Submits a Runnable task for execution and returns a Future
109 * representing that task.
110 *
111 * @param task the task to submit
112 * @param result the result to return upon completion
113 * If you do not need a particular result, consider using
114 * the form: <tt>Future&lt;?&gt; cancellationHandle = e.submit(task, null)</tt>.
115 * @return a Future representing pending completion of the task,
116 * and whose <tt>get()</tt> method will return the given value
117 * upon completion
118 * @throws RejectedExecutionException if task cannot be scheduled
119 * for execution
120 */
121 <T> Future<T> submit(Runnable task, T result);
122
123 /**
124 * Submits a value-returning task for execution and returns a Future
125 * representing the pending results of the task.
126 *
127 * @param task the task to submit
128 * @return a Future representing pending completion of the task
129 * @throws RejectedExecutionException if task cannot be scheduled
130 * for execution
131 */
132 <T> Future<T> submit(Callable<T> task);
133
134
135 /**
136 * Submits a privileged action for execution under the current
137 * access control context and returns a Future representing the
138 * pending result object of that action.
139 *
140 * @param action the action to submit
141 * @return a Future representing pending completion of the action
142 * @throws RejectedExecutionException if action cannot be scheduled
143 * for execution
144 */
145 Future<Object> submit(PrivilegedAction action);
146
147 /**
148 * Submits a privileged exception action for execution under the current
149 * access control context and returns a Future representing the pending
150 * result object of that action.
151 *
152 * @param action the action to submit
153 * @return a Future representing pending completion of the action
154 * @throws RejectedExecutionException if action cannot be scheduled
155 * for execution
156 */
157 Future<Object> submit(PrivilegedExceptionAction action);
158
159 /**
160 * Executes a Runnable task and blocks until it completes normally
161 * or throws an exception.
162 *
163 * @param task the task to submit
164 * @throws RejectedExecutionException if task cannot be scheduled
165 * for execution
166 * @throws ExecutionException if the task encountered an exception
167 * while executing
168 */
169 void invoke(Runnable task) throws ExecutionException, InterruptedException;
170
171 /**
172 * Executes a value-returning task and blocks until it returns a
173 * value or throws an exception.
174 *
175 * @param task the task to submit
176 * @return a Future representing pending completion of the task
177 * @throws RejectedExecutionException if task cannot be scheduled
178 * for execution
179 * @throws InterruptedException if interrupted while waiting for
180 * completion
181 * @throws ExecutionException if the task encountered an exception
182 * while executing
183 */
184 <T> T invoke(Callable<T> task) throws ExecutionException, InterruptedException;
185
186 /**
187 * Executes the given tasks, returning when
188 * all of them complete.
189 * Note that a <em>completed</em> task could have
190 * terminated either normally or by throwing an exception.
191 * @param tasks the collection of tasks
192 * @param result value for each task to return upon completion.
193 * If you do not need a particular result, consider using <tt>null</tt>.
194 * @return A list of Futures representing the tasks, in the same
195 * sequential order as as produced by the iterator for the given task list, each of which has
196 * completed.
197 * @throws InterruptedException if interrupted while waiting, in
198 * which case unfinished tasks are cancelled.
199 * @throws NullPointerException if tasks or any of its elements are <tt>null</tt>
200 * @throws RejectedExecutionException if any task cannot be scheduled
201 * for execution
202 */
203 <T> List<Future<T>> invokeAll(Collection<Runnable> tasks, T result)
204 throws InterruptedException;
205
206 /**
207 * Executes the given tasks, returning normally
208 * when all complete or the given timeout expires, whichever
209 * happens first.
210 * Upon return, tasks that have not completed are cancelled.
211 * Note that a <em>completed</em> task could have
212 * terminated either normally or by throwing an exception.
213 * @param tasks the collection of tasks
214 * @param result value for each task to return upon completion.
215 * If you do not need a particular result, consider using <tt>null</tt>.
216 * @param timeout the maximum time to wait
217 * @return A list of Futures representing the tasks, in the same
218 * sequential order as as produced by the iterator for the given task list. If the operation did
219 * not time out, each task will have completed. If it did time
220 * out, some of these tasks will not have completed.
221 * @param unit the time unit of the timeout argument
222 * @throws InterruptedException if interrupted while waiting, in
223 * which case unfinished tasks are cancelled.
224 * @throws NullPointerException if tasks, any of its elements, or unit are <tt>null</tt>
225 * @throws RejectedExecutionException if any task cannot be scheduled
226 * for execution
227 */
228 <T> List<Future<T>> invokeAll(Collection<Runnable> tasks, T result,
229 long timeout, TimeUnit unit)
230 throws InterruptedException;
231
232
233 /**
234 * Executes the given tasks, returning their results
235 * when all complete.
236 * Note that a <em>completed</em> task could have
237 * terminated either normally or by throwing an exception.
238 * @param tasks the collection of tasks
239 * @return A list of Futures representing the tasks, in the same
240 * sequential order as produced by the iterator for the given task list, each of which has
241 * completed.
242 * @throws InterruptedException if interrupted while waiting, in
243 * which case unfinished tasks are cancelled.
244 * @throws NullPointerException if tasks or any of its elements are <tt>null</tt>
245 * @throws RejectedExecutionException if any task cannot be scheduled
246 * for execution
247 */
248
249 <T> List<Future<T>> invokeAll(Collection<Callable<T>> tasks)
250 throws InterruptedException;
251
252 /**
253 * Executes the given tasks, returning their results
254 * when all complete or the timeout expires, whichever happens first.
255 * Upon return, tasks that have not completed are cancelled.
256 * Note that a <em>completed</em> task could have
257 * terminated either normally or by throwing an exception.
258 * @param tasks the collection of tasks
259 * @param timeout the maximum time to wait
260 * @param unit the time unit of the timeout argument
261 * @return A list of Futures representing the tasks, in the same
262 * sequential order as as produced by the iterator for the given task list. If the operation did
263 * not time out, each task will have completed. If it did time
264 * out, some of thiese tasks will not have completed.
265 * @throws InterruptedException if interrupted while waiting, in
266 * which case unfinished tasks are cancelled.
267 * @throws NullPointerException if tasks, any of its elements, or
268 * unit are <tt>null</tt>
269 * @throws RejectedExecutionException if any task cannot be scheduled
270 * for execution
271 */
272 <T> List<Future<T>> invokeAll(Collection<Callable<T>> tasks,
273 long timeout, TimeUnit unit)
274 throws InterruptedException;
275
276
277 /**
278 * Executes the given tasks, returning the result
279 * of one that has completed successfully (i.e., without throwing
280 * an exception), if any do. Upon normal or exceptional return,
281 * tasks that have not completed are cancelled.
282 * @param tasks the collection of tasks
283 * @return The result returned by one of the tasks.
284 * @throws InterruptedException if interrupted while waiting
285 * @throws NullPointerException if tasks or any of its elements
286 * are <tt>null</tt>
287 * @throws IllegalArgumentException if tasks empty
288 * @throws ExecutionException if no task successfully completes
289 * @throws RejectedExecutionException if tasks cannot be scheduled
290 * for execution
291 */
292
293 <T> T invokeAny(Collection<Callable<T>> tasks)
294 throws InterruptedException, ExecutionException;
295
296 /**
297 * Executes the given tasks, returning the result
298 * of one that has completed successfully (i.e., without throwing
299 * an exception), if any do before the given timeout elapses.
300 * Upon normal or exceptional return, tasks that have not
301 * completed are cancelled.
302 * @param tasks the collection of tasks
303 * @param timeout the maximum time to wait
304 * @param unit the time unit of the timeout argument
305 * @return The result returned by one of the tasks.
306 * @throws InterruptedException if interrupted while waiting
307 * @throws NullPointerException if tasks, any of its elements, or
308 * unit are <tt>null</tt>
309 * @throws TimeoutException if the given timeout elapses before
310 * any task successfully completes
311 * @throws ExecutionException if no task successfully completes
312 * @throws RejectedExecutionException if tasks cannot be scheduled
313 * for execution
314 */
315 <T> T invokeAny(Collection<Callable<T>> tasks,
316 long timeout, TimeUnit unit)
317 throws InterruptedException, ExecutionException, TimeoutException;
318
319 /**
320 * Executes the given tasks, returning the given
321 * result if one completes successfully (i.e., without
322 * throwing an exception). Upon normal or exceptional
323 * return, tasks that have not completed are cancelled.
324 * @param tasks the collection of tasks
325 * @param result the result to return upon successful completion
326 * @return the given result
327 * @throws InterruptedException if interrupted while waiting
328 * @throws NullPointerException if tasks or any of its elements
329 * are <tt>null</tt>
330 * @throws IllegalArgumentException if tasks empty
331 * @throws ExecutionException if no task successfully completes
332 * @throws RejectedExecutionException if tasks cannot be scheduled
333 * for execution
334 */
335
336 <T> T invokeAny(Collection<Runnable> tasks, T result)
337 throws InterruptedException, ExecutionException;
338
339 /**
340 * Executes the given tasks, returning the given result
341 * if one completes successfully (i.e., without throwing
342 * an exception) before the given timeout elapses.
343 * Upon normal or exceptional return, tasks that have not
344 * completed are cancelled.
345 * @param tasks the collection of tasks
346 * @param result the result to return upon successful completion
347 * @param timeout the maximum time to wait
348 * @param unit the time unit of the timeout argument
349 * @return the given result
350 * @throws InterruptedException if interrupted while waiting
351 * @throws NullPointerException if tasks, any of its elements, or
352 * unit are <tt>null</tt>
353 * @throws TimeoutException if the given timeout elapses before
354 * any task successfully completes
355 * @throws ExecutionException if no task successfully completes
356 * @throws RejectedExecutionException if tasks cannot be scheduled
357 * for execution
358 */
359 <T> T invokeAny(Collection<Runnable> tasks, T result,
360 long timeout, TimeUnit unit)
361 throws InterruptedException, ExecutionException, TimeoutException;
362
363
364 }