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