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