author | psandoz |
Tue, 09 Jul 2013 16:04:25 +0200 | |
changeset 18790 | d25399d849bc |
parent 14325 | 622c473a21aa |
child 19048 | 7d0a94c79779 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
3 |
* |
|
4 |
* This code is free software; you can redistribute it and/or modify it |
|
5 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 6 |
* published by the Free Software Foundation. Oracle designates this |
2 | 7 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 8 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 9 |
* |
10 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
11 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
12 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
13 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
14 |
* accompanied this code). |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License version |
|
17 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
18 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
19 |
* |
|
5506 | 20 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
21 |
* or visit www.oracle.com if you need additional information or have any |
|
22 |
* questions. |
|
2 | 23 |
*/ |
24 |
||
25 |
/* |
|
26 |
* This file is available under and governed by the GNU General Public |
|
27 |
* License version 2 only, as published by the Free Software Foundation. |
|
28 |
* However, the following notice accompanied the original version of this |
|
29 |
* file: |
|
30 |
* |
|
31 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
|
32 |
* Expert Group and released to the public domain, as explained at |
|
9242
ef138d47df58
7034657: Update Creative Commons license URL in legal notices
dl
parents:
5506
diff
changeset
|
33 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
2 | 34 |
*/ |
35 |
||
36 |
package java.util.concurrent; |
|
37 |
import java.util.List; |
|
38 |
import java.util.Collection; |
|
39 |
||
40 |
/** |
|
41 |
* An {@link Executor} that provides methods to manage termination and |
|
42 |
* methods that can produce a {@link Future} for tracking progress of |
|
43 |
* one or more asynchronous tasks. |
|
44 |
* |
|
18790 | 45 |
* <p>An {@code ExecutorService} can be shut down, which will cause |
2 | 46 |
* it to reject new tasks. Two different methods are provided for |
18790 | 47 |
* shutting down an {@code ExecutorService}. The {@link #shutdown} |
2 | 48 |
* method will allow previously submitted tasks to execute before |
49 |
* terminating, while the {@link #shutdownNow} method prevents waiting |
|
50 |
* tasks from starting and attempts to stop currently executing tasks. |
|
51 |
* Upon termination, an executor has no tasks actively executing, no |
|
52 |
* tasks awaiting execution, and no new tasks can be submitted. An |
|
18790 | 53 |
* unused {@code ExecutorService} should be shut down to allow |
2 | 54 |
* reclamation of its resources. |
55 |
* |
|
18790 | 56 |
* <p>Method {@code submit} extends base method {@link |
57 |
* Executor#execute(Runnable)} by creating and returning a {@link Future} |
|
58 |
* that can be used to cancel execution and/or wait for completion. |
|
59 |
* Methods {@code invokeAny} and {@code invokeAll} perform the most |
|
2 | 60 |
* commonly useful forms of bulk execution, executing a collection of |
61 |
* tasks and then waiting for at least one, or all, to |
|
62 |
* complete. (Class {@link ExecutorCompletionService} can be used to |
|
63 |
* write customized variants of these methods.) |
|
64 |
* |
|
65 |
* <p>The {@link Executors} class provides factory methods for the |
|
66 |
* executor services provided in this package. |
|
67 |
* |
|
68 |
* <h3>Usage Examples</h3> |
|
69 |
* |
|
70 |
* Here is a sketch of a network service in which threads in a thread |
|
71 |
* pool service incoming requests. It uses the preconfigured {@link |
|
72 |
* Executors#newFixedThreadPool} factory method: |
|
73 |
* |
|
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
9242
diff
changeset
|
74 |
* <pre> {@code |
2 | 75 |
* class NetworkService implements Runnable { |
76 |
* private final ServerSocket serverSocket; |
|
77 |
* private final ExecutorService pool; |
|
78 |
* |
|
79 |
* public NetworkService(int port, int poolSize) |
|
80 |
* throws IOException { |
|
81 |
* serverSocket = new ServerSocket(port); |
|
82 |
* pool = Executors.newFixedThreadPool(poolSize); |
|
83 |
* } |
|
84 |
* |
|
85 |
* public void run() { // run the service |
|
86 |
* try { |
|
87 |
* for (;;) { |
|
88 |
* pool.execute(new Handler(serverSocket.accept())); |
|
89 |
* } |
|
90 |
* } catch (IOException ex) { |
|
91 |
* pool.shutdown(); |
|
92 |
* } |
|
93 |
* } |
|
94 |
* } |
|
95 |
* |
|
96 |
* class Handler implements Runnable { |
|
97 |
* private final Socket socket; |
|
98 |
* Handler(Socket socket) { this.socket = socket; } |
|
99 |
* public void run() { |
|
100 |
* // read and service request on socket |
|
101 |
* } |
|
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
9242
diff
changeset
|
102 |
* }}</pre> |
2 | 103 |
* |
18790 | 104 |
* The following method shuts down an {@code ExecutorService} in two phases, |
105 |
* first by calling {@code shutdown} to reject incoming tasks, and then |
|
106 |
* calling {@code shutdownNow}, if necessary, to cancel any lingering tasks: |
|
2 | 107 |
* |
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
9242
diff
changeset
|
108 |
* <pre> {@code |
2 | 109 |
* void shutdownAndAwaitTermination(ExecutorService pool) { |
110 |
* pool.shutdown(); // Disable new tasks from being submitted |
|
111 |
* try { |
|
112 |
* // Wait a while for existing tasks to terminate |
|
113 |
* if (!pool.awaitTermination(60, TimeUnit.SECONDS)) { |
|
114 |
* pool.shutdownNow(); // Cancel currently executing tasks |
|
115 |
* // Wait a while for tasks to respond to being cancelled |
|
116 |
* if (!pool.awaitTermination(60, TimeUnit.SECONDS)) |
|
117 |
* System.err.println("Pool did not terminate"); |
|
118 |
* } |
|
119 |
* } catch (InterruptedException ie) { |
|
120 |
* // (Re-)Cancel if current thread also interrupted |
|
121 |
* pool.shutdownNow(); |
|
122 |
* // Preserve interrupt status |
|
123 |
* Thread.currentThread().interrupt(); |
|
124 |
* } |
|
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
9242
diff
changeset
|
125 |
* }}</pre> |
2 | 126 |
* |
127 |
* <p>Memory consistency effects: Actions in a thread prior to the |
|
128 |
* submission of a {@code Runnable} or {@code Callable} task to an |
|
129 |
* {@code ExecutorService} |
|
130 |
* <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a> |
|
131 |
* any actions taken by that task, which in turn <i>happen-before</i> the |
|
132 |
* result is retrieved via {@code Future.get()}. |
|
133 |
* |
|
134 |
* @since 1.5 |
|
135 |
* @author Doug Lea |
|
136 |
*/ |
|
137 |
public interface ExecutorService extends Executor { |
|
138 |
||
139 |
/** |
|
140 |
* Initiates an orderly shutdown in which previously submitted |
|
141 |
* tasks are executed, but no new tasks will be accepted. |
|
142 |
* Invocation has no additional effect if already shut down. |
|
143 |
* |
|
61
5691b03db1ea
6620549: ExecutorService#shutdown should clearly state that it does not block
martin
parents:
2
diff
changeset
|
144 |
* <p>This method does not wait for previously submitted tasks to |
5691b03db1ea
6620549: ExecutorService#shutdown should clearly state that it does not block
martin
parents:
2
diff
changeset
|
145 |
* complete execution. Use {@link #awaitTermination awaitTermination} |
5691b03db1ea
6620549: ExecutorService#shutdown should clearly state that it does not block
martin
parents:
2
diff
changeset
|
146 |
* to do that. |
5691b03db1ea
6620549: ExecutorService#shutdown should clearly state that it does not block
martin
parents:
2
diff
changeset
|
147 |
* |
2 | 148 |
* @throws SecurityException if a security manager exists and |
149 |
* shutting down this ExecutorService may manipulate |
|
150 |
* threads that the caller is not permitted to modify |
|
151 |
* because it does not hold {@link |
|
18790 | 152 |
* java.lang.RuntimePermission}{@code ("modifyThread")}, |
153 |
* or the security manager's {@code checkAccess} method |
|
2 | 154 |
* denies access. |
155 |
*/ |
|
156 |
void shutdown(); |
|
157 |
||
158 |
/** |
|
159 |
* Attempts to stop all actively executing tasks, halts the |
|
61
5691b03db1ea
6620549: ExecutorService#shutdown should clearly state that it does not block
martin
parents:
2
diff
changeset
|
160 |
* processing of waiting tasks, and returns a list of the tasks |
5691b03db1ea
6620549: ExecutorService#shutdown should clearly state that it does not block
martin
parents:
2
diff
changeset
|
161 |
* that were awaiting execution. |
5691b03db1ea
6620549: ExecutorService#shutdown should clearly state that it does not block
martin
parents:
2
diff
changeset
|
162 |
* |
5691b03db1ea
6620549: ExecutorService#shutdown should clearly state that it does not block
martin
parents:
2
diff
changeset
|
163 |
* <p>This method does not wait for actively executing tasks to |
5691b03db1ea
6620549: ExecutorService#shutdown should clearly state that it does not block
martin
parents:
2
diff
changeset
|
164 |
* terminate. Use {@link #awaitTermination awaitTermination} to |
5691b03db1ea
6620549: ExecutorService#shutdown should clearly state that it does not block
martin
parents:
2
diff
changeset
|
165 |
* do that. |
2 | 166 |
* |
167 |
* <p>There are no guarantees beyond best-effort attempts to stop |
|
168 |
* processing actively executing tasks. For example, typical |
|
169 |
* implementations will cancel via {@link Thread#interrupt}, so any |
|
170 |
* task that fails to respond to interrupts may never terminate. |
|
171 |
* |
|
172 |
* @return list of tasks that never commenced execution |
|
173 |
* @throws SecurityException if a security manager exists and |
|
174 |
* shutting down this ExecutorService may manipulate |
|
175 |
* threads that the caller is not permitted to modify |
|
176 |
* because it does not hold {@link |
|
18790 | 177 |
* java.lang.RuntimePermission}{@code ("modifyThread")}, |
178 |
* or the security manager's {@code checkAccess} method |
|
2 | 179 |
* denies access. |
180 |
*/ |
|
181 |
List<Runnable> shutdownNow(); |
|
182 |
||
183 |
/** |
|
18790 | 184 |
* Returns {@code true} if this executor has been shut down. |
2 | 185 |
* |
18790 | 186 |
* @return {@code true} if this executor has been shut down |
2 | 187 |
*/ |
188 |
boolean isShutdown(); |
|
189 |
||
190 |
/** |
|
18790 | 191 |
* Returns {@code true} if all tasks have completed following shut down. |
192 |
* Note that {@code isTerminated} is never {@code true} unless |
|
193 |
* either {@code shutdown} or {@code shutdownNow} was called first. |
|
2 | 194 |
* |
18790 | 195 |
* @return {@code true} if all tasks have completed following shut down |
2 | 196 |
*/ |
197 |
boolean isTerminated(); |
|
198 |
||
199 |
/** |
|
200 |
* Blocks until all tasks have completed execution after a shutdown |
|
201 |
* request, or the timeout occurs, or the current thread is |
|
202 |
* interrupted, whichever happens first. |
|
203 |
* |
|
204 |
* @param timeout the maximum time to wait |
|
205 |
* @param unit the time unit of the timeout argument |
|
18790 | 206 |
* @return {@code true} if this executor terminated and |
207 |
* {@code false} if the timeout elapsed before termination |
|
2 | 208 |
* @throws InterruptedException if interrupted while waiting |
209 |
*/ |
|
210 |
boolean awaitTermination(long timeout, TimeUnit unit) |
|
211 |
throws InterruptedException; |
|
212 |
||
213 |
/** |
|
214 |
* Submits a value-returning task for execution and returns a |
|
215 |
* Future representing the pending results of the task. The |
|
18790 | 216 |
* Future's {@code get} method will return the task's result upon |
2 | 217 |
* successful completion. |
218 |
* |
|
219 |
* <p> |
|
220 |
* If you would like to immediately block waiting |
|
221 |
* for a task, you can use constructions of the form |
|
18790 | 222 |
* {@code result = exec.submit(aCallable).get();} |
2 | 223 |
* |
18790 | 224 |
* <p>Note: The {@link Executors} class includes a set of methods |
2 | 225 |
* that can convert some other common closure-like objects, |
226 |
* for example, {@link java.security.PrivilegedAction} to |
|
227 |
* {@link Callable} form so they can be submitted. |
|
228 |
* |
|
229 |
* @param task the task to submit |
|
230 |
* @return a Future representing pending completion of the task |
|
231 |
* @throws RejectedExecutionException if the task cannot be |
|
232 |
* scheduled for execution |
|
233 |
* @throws NullPointerException if the task is null |
|
234 |
*/ |
|
235 |
<T> Future<T> submit(Callable<T> task); |
|
236 |
||
237 |
/** |
|
238 |
* Submits a Runnable task for execution and returns a Future |
|
18790 | 239 |
* representing that task. The Future's {@code get} method will |
2 | 240 |
* return the given result upon successful completion. |
241 |
* |
|
242 |
* @param task the task to submit |
|
243 |
* @param result the result to return |
|
244 |
* @return a Future representing pending completion of the task |
|
245 |
* @throws RejectedExecutionException if the task cannot be |
|
246 |
* scheduled for execution |
|
247 |
* @throws NullPointerException if the task is null |
|
248 |
*/ |
|
249 |
<T> Future<T> submit(Runnable task, T result); |
|
250 |
||
251 |
/** |
|
252 |
* Submits a Runnable task for execution and returns a Future |
|
18790 | 253 |
* representing that task. The Future's {@code get} method will |
254 |
* return {@code null} upon <em>successful</em> completion. |
|
2 | 255 |
* |
256 |
* @param task the task to submit |
|
257 |
* @return a Future representing pending completion of the task |
|
258 |
* @throws RejectedExecutionException if the task cannot be |
|
259 |
* scheduled for execution |
|
260 |
* @throws NullPointerException if the task is null |
|
261 |
*/ |
|
262 |
Future<?> submit(Runnable task); |
|
263 |
||
264 |
/** |
|
265 |
* Executes the given tasks, returning a list of Futures holding |
|
266 |
* their status and results when all complete. |
|
18790 | 267 |
* {@link Future#isDone} is {@code true} for each |
2 | 268 |
* element of the returned list. |
269 |
* Note that a <em>completed</em> task could have |
|
270 |
* terminated either normally or by throwing an exception. |
|
271 |
* The results of this method are undefined if the given |
|
272 |
* collection is modified while this operation is in progress. |
|
273 |
* |
|
274 |
* @param tasks the collection of tasks |
|
18790 | 275 |
* @return a list of Futures representing the tasks, in the same |
2 | 276 |
* sequential order as produced by the iterator for the |
18790 | 277 |
* given task list, each of which has completed |
2 | 278 |
* @throws InterruptedException if interrupted while waiting, in |
18790 | 279 |
* which case unfinished tasks are cancelled |
280 |
* @throws NullPointerException if tasks or any of its elements are {@code null} |
|
2 | 281 |
* @throws RejectedExecutionException if any task cannot be |
282 |
* scheduled for execution |
|
283 |
*/ |
|
284 |
<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) |
|
285 |
throws InterruptedException; |
|
286 |
||
287 |
/** |
|
288 |
* Executes the given tasks, returning a list of Futures holding |
|
289 |
* their status and results |
|
290 |
* when all complete or the timeout expires, whichever happens first. |
|
18790 | 291 |
* {@link Future#isDone} is {@code true} for each |
2 | 292 |
* element of the returned list. |
293 |
* Upon return, tasks that have not completed are cancelled. |
|
294 |
* Note that a <em>completed</em> task could have |
|
295 |
* terminated either normally or by throwing an exception. |
|
296 |
* The results of this method are undefined if the given |
|
297 |
* collection is modified while this operation is in progress. |
|
298 |
* |
|
299 |
* @param tasks the collection of tasks |
|
300 |
* @param timeout the maximum time to wait |
|
301 |
* @param unit the time unit of the timeout argument |
|
302 |
* @return a list of Futures representing the tasks, in the same |
|
303 |
* sequential order as produced by the iterator for the |
|
304 |
* given task list. If the operation did not time out, |
|
305 |
* each task will have completed. If it did time out, some |
|
306 |
* of these tasks will not have completed. |
|
307 |
* @throws InterruptedException if interrupted while waiting, in |
|
308 |
* which case unfinished tasks are cancelled |
|
309 |
* @throws NullPointerException if tasks, any of its elements, or |
|
18790 | 310 |
* unit are {@code null} |
2 | 311 |
* @throws RejectedExecutionException if any task cannot be scheduled |
312 |
* for execution |
|
313 |
*/ |
|
314 |
<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, |
|
315 |
long timeout, TimeUnit unit) |
|
316 |
throws InterruptedException; |
|
317 |
||
318 |
/** |
|
319 |
* Executes the given tasks, returning the result |
|
320 |
* of one that has completed successfully (i.e., without throwing |
|
321 |
* an exception), if any do. Upon normal or exceptional return, |
|
322 |
* tasks that have not completed are cancelled. |
|
323 |
* The results of this method are undefined if the given |
|
324 |
* collection is modified while this operation is in progress. |
|
325 |
* |
|
326 |
* @param tasks the collection of tasks |
|
327 |
* @return the result returned by one of the tasks |
|
328 |
* @throws InterruptedException if interrupted while waiting |
|
4110 | 329 |
* @throws NullPointerException if tasks or any element task |
18790 | 330 |
* subject to execution is {@code null} |
2 | 331 |
* @throws IllegalArgumentException if tasks is empty |
332 |
* @throws ExecutionException if no task successfully completes |
|
333 |
* @throws RejectedExecutionException if tasks cannot be scheduled |
|
334 |
* for execution |
|
335 |
*/ |
|
336 |
<T> T invokeAny(Collection<? extends Callable<T>> tasks) |
|
337 |
throws InterruptedException, ExecutionException; |
|
338 |
||
339 |
/** |
|
340 |
* Executes the given tasks, returning the result |
|
341 |
* of one that has completed successfully (i.e., without throwing |
|
342 |
* an exception), if any do before the given timeout elapses. |
|
343 |
* Upon normal or exceptional return, tasks that have not |
|
344 |
* completed are cancelled. |
|
345 |
* The results of this method are undefined if the given |
|
346 |
* collection is modified while this operation is in progress. |
|
347 |
* |
|
348 |
* @param tasks the collection of tasks |
|
349 |
* @param timeout the maximum time to wait |
|
350 |
* @param unit the time unit of the timeout argument |
|
18790 | 351 |
* @return the result returned by one of the tasks |
2 | 352 |
* @throws InterruptedException if interrupted while waiting |
4110 | 353 |
* @throws NullPointerException if tasks, or unit, or any element |
18790 | 354 |
* task subject to execution is {@code null} |
2 | 355 |
* @throws TimeoutException if the given timeout elapses before |
356 |
* any task successfully completes |
|
357 |
* @throws ExecutionException if no task successfully completes |
|
358 |
* @throws RejectedExecutionException if tasks cannot be scheduled |
|
359 |
* for execution |
|
360 |
*/ |
|
361 |
<T> T invokeAny(Collection<? extends Callable<T>> tasks, |
|
362 |
long timeout, TimeUnit unit) |
|
363 |
throws InterruptedException, ExecutionException, TimeoutException; |
|
364 |
} |