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