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