ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/ExecutorService.java
Revision: 1.13
Committed: Mon Dec 15 00:29:49 2003 UTC (20 years, 5 months ago) by dl
Branch: MAIN
Changes since 1.12: +65 -44 lines
Log Message:
Added CompletionService; Executor any/all methods now require lists

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