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