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. <p> |
18 |
* |
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) throws IOException { |
49 |
* serverSocket = new ServerSocket(port); |
50 |
* pool = Executors.newFixedThreadPool(poolSize); |
51 |
* } |
52 |
* |
53 |
* public void serve() { |
54 |
* try { |
55 |
* for (;;) { |
56 |
* pool.execute(new Handler(serverSocket.accept())); |
57 |
* } |
58 |
* } catch (IOException ex) { |
59 |
* pool.shutdown(); |
60 |
* } |
61 |
* } |
62 |
* } |
63 |
* |
64 |
* class Handler implements Runnable { |
65 |
* private final Socket socket; |
66 |
* Handler(Socket socket) { this.socket = socket; } |
67 |
* public void run() { |
68 |
* // read and service request |
69 |
* } |
70 |
* } |
71 |
* </pre> |
72 |
* @since 1.5 |
73 |
* @author Doug Lea |
74 |
*/ |
75 |
public interface ExecutorService extends Executor { |
76 |
|
77 |
/** |
78 |
* Initiates an orderly shutdown in which previously submitted |
79 |
* tasks are executed, but no new tasks will be |
80 |
* accepted. Invocation has no additional effect if already shut |
81 |
* down. |
82 |
* @throws SecurityException if a security manager exists and |
83 |
* shutting down this ExecutorService may manipulate threads that |
84 |
* the caller is not permitted to modify because it does not hold |
85 |
* {@link java.lang.RuntimePermission}<tt>("modifyThread")</tt>, |
86 |
* or the security manager's <tt>checkAccess</tt> method denies access. |
87 |
*/ |
88 |
void shutdown(); |
89 |
|
90 |
/** |
91 |
* Attempts to stop all actively executing tasks, halts the |
92 |
* processing of waiting tasks, and returns a list of the tasks that were |
93 |
* awaiting execution. |
94 |
* |
95 |
* <p>There are no guarantees beyond best-effort attempts to stop |
96 |
* processing actively executing tasks. For example, typical |
97 |
* implementations will cancel via {@link Thread#interrupt}, so if any |
98 |
* tasks mask or fail to respond to interrupts, they may never terminate. |
99 |
* |
100 |
* @return list of tasks that never commenced execution |
101 |
* @throws SecurityException if a security manager exists and |
102 |
* shutting down this ExecutorService may manipulate threads that |
103 |
* the caller is not permitted to modify because it does not hold |
104 |
* {@link java.lang.RuntimePermission}<tt>("modifyThread")</tt>, |
105 |
* or the security manager's <tt>checkAccess</tt> method denies access. |
106 |
*/ |
107 |
List<Runnable> shutdownNow(); |
108 |
|
109 |
/** |
110 |
* Returns <tt>true</tt> if this executor has been shut down. |
111 |
* |
112 |
* @return <tt>true</tt> if this executor has been shut down |
113 |
*/ |
114 |
boolean isShutdown(); |
115 |
|
116 |
/** |
117 |
* Returns <tt>true</tt> if all tasks have completed following shut down. |
118 |
* Note that <tt>isTerminated</tt> is never <tt>true</tt> unless |
119 |
* either <tt>shutdown</tt> or <tt>shutdownNow</tt> was called first. |
120 |
* |
121 |
* @return <tt>true</tt> if all tasks have completed following shut down |
122 |
*/ |
123 |
boolean isTerminated(); |
124 |
|
125 |
/** |
126 |
* Blocks until all tasks have completed execution after a shutdown |
127 |
* request, or the timeout occurs, or the current thread is |
128 |
* interrupted, whichever happens first. |
129 |
* |
130 |
* @param timeout the maximum time to wait |
131 |
* @param unit the time unit of the timeout argument |
132 |
* @return <tt>true</tt> if this executor terminated and <tt>false</tt> |
133 |
* if the timeout elapsed before termination |
134 |
* @throws InterruptedException if interrupted while waiting |
135 |
*/ |
136 |
boolean awaitTermination(long timeout, TimeUnit unit) |
137 |
throws InterruptedException; |
138 |
|
139 |
|
140 |
/** |
141 |
* Submits a value-returning task for execution and returns a Future |
142 |
* representing the pending results of the task. |
143 |
* |
144 |
* <p> |
145 |
* If you would like to immediately block waiting |
146 |
* for a task, you can use constructions of the form |
147 |
* <tt>result = exec.submit(aCallable).get();</tt> |
148 |
* |
149 |
* <p> Note: The {@link Executors} class includes a set of methods |
150 |
* that can convert some other common closure-like objects, |
151 |
* for example, {@link java.security.PrivilegedAction} to |
152 |
* {@link Callable} form so they can be submitted. |
153 |
* |
154 |
* @param task the task to submit |
155 |
* @return a Future representing pending completion of the task |
156 |
* @throws RejectedExecutionException if task cannot be scheduled |
157 |
* for execution |
158 |
* @throws NullPointerException if task null |
159 |
*/ |
160 |
<T> Future<T> submit(Callable<T> task); |
161 |
|
162 |
/** |
163 |
* Submits a Runnable task for execution and returns a Future |
164 |
* representing that task that will upon completion return |
165 |
* the given result |
166 |
* |
167 |
* @param task the task to submit |
168 |
* @param result the result to return |
169 |
* @return a Future representing pending completion of the task, |
170 |
* and whose <tt>get()</tt> method will return the given result |
171 |
* upon completion. |
172 |
* @throws RejectedExecutionException if task cannot be scheduled |
173 |
* for execution |
174 |
* @throws NullPointerException if task null |
175 |
*/ |
176 |
<T> Future<T> submit(Runnable task, T result); |
177 |
|
178 |
/** |
179 |
* Submits a Runnable task for execution and returns a Future |
180 |
* representing that task. |
181 |
* |
182 |
* @param task the task to submit |
183 |
* @return a Future representing pending completion of the task, |
184 |
* and whose <tt>get()</tt> method will return <tt>null</tt> |
185 |
* upon completion. |
186 |
* @throws RejectedExecutionException if task cannot be scheduled |
187 |
* for execution |
188 |
* @throws NullPointerException if task null |
189 |
*/ |
190 |
Future<?> submit(Runnable task); |
191 |
|
192 |
/** |
193 |
* Executes the given tasks, returning their results |
194 |
* when all complete. |
195 |
* Note that a <em>completed</em> task could have |
196 |
* terminated either normally or by throwing an exception. |
197 |
* The results of this method are undefined if the given |
198 |
* collection is modified while this operation is in progress. |
199 |
* @param tasks the collection of tasks |
200 |
* @return A list of Futures representing the tasks, in the same |
201 |
* sequential order as produced by the iterator for the given task |
202 |
* list, each of which has completed. |
203 |
* @throws InterruptedException if interrupted while waiting, in |
204 |
* which case unfinished tasks are cancelled. |
205 |
* @throws NullPointerException if tasks or any of its elements are <tt>null</tt> |
206 |
* @throws RejectedExecutionException if any task cannot be scheduled |
207 |
* for execution |
208 |
*/ |
209 |
|
210 |
<T> List<Future<T>> invokeAll(Collection<Callable<T>> tasks) |
211 |
throws InterruptedException; |
212 |
|
213 |
/** |
214 |
* Executes the given tasks, returning their results |
215 |
* when all complete or the timeout expires, whichever happens first. |
216 |
* Upon return, tasks that have not completed are cancelled. |
217 |
* Note that a <em>completed</em> task could have |
218 |
* terminated either normally or by throwing an exception. |
219 |
* The results of this method are undefined if the given |
220 |
* collection is modified while this operation is in progress. |
221 |
* @param tasks the collection of tasks |
222 |
* @param timeout the maximum time to wait |
223 |
* @param unit the time unit of the timeout argument |
224 |
* @return A list of Futures representing the tasks, in the same |
225 |
* sequential order as as produced by the iterator for the given |
226 |
* task list. If the operation did not time out, each task will |
227 |
* have completed. If it did time out, some of thiese tasks will |
228 |
* not have completed. |
229 |
* @throws InterruptedException if interrupted while waiting, in |
230 |
* which case unfinished tasks are cancelled. |
231 |
* @throws NullPointerException if tasks, any of its elements, or |
232 |
* unit are <tt>null</tt> |
233 |
* @throws RejectedExecutionException if any task cannot be scheduled |
234 |
* for execution |
235 |
*/ |
236 |
<T> List<Future<T>> invokeAll(Collection<Callable<T>> tasks, |
237 |
long timeout, TimeUnit unit) |
238 |
throws InterruptedException; |
239 |
|
240 |
/** |
241 |
* Executes the given tasks, returning the result |
242 |
* of one that has completed successfully (i.e., without throwing |
243 |
* an exception), if any do. Upon normal or exceptional return, |
244 |
* tasks that have not completed are cancelled. |
245 |
* The results of this method are undefined if the given |
246 |
* collection is modified while this operation is in progress. |
247 |
* @param tasks the collection of tasks |
248 |
* @return The result returned by one of the tasks. |
249 |
* @throws InterruptedException if interrupted while waiting |
250 |
* @throws NullPointerException if tasks or any of its elements |
251 |
* are <tt>null</tt> |
252 |
* @throws IllegalArgumentException if tasks empty |
253 |
* @throws ExecutionException if no task successfully completes |
254 |
* @throws RejectedExecutionException if tasks cannot be scheduled |
255 |
* for execution |
256 |
*/ |
257 |
<T> T invokeAny(Collection<Callable<T>> tasks) |
258 |
throws InterruptedException, ExecutionException; |
259 |
|
260 |
/** |
261 |
* Executes the given tasks, returning the result |
262 |
* of one that has completed successfully (i.e., without throwing |
263 |
* an exception), if any do before the given timeout elapses. |
264 |
* Upon normal or exceptional return, tasks that have not |
265 |
* completed are cancelled. |
266 |
* The results of this method are undefined if the given |
267 |
* collection is modified while this operation is in progress. |
268 |
* @param tasks the collection of tasks |
269 |
* @param timeout the maximum time to wait |
270 |
* @param unit the time unit of the timeout argument |
271 |
* @return The result returned by one of the tasks. |
272 |
* @throws InterruptedException if interrupted while waiting |
273 |
* @throws NullPointerException if tasks, any of its elements, or |
274 |
* unit are <tt>null</tt> |
275 |
* @throws TimeoutException if the given timeout elapses before |
276 |
* any task successfully completes |
277 |
* @throws ExecutionException if no task successfully completes |
278 |
* @throws RejectedExecutionException if tasks cannot be scheduled |
279 |
* for execution |
280 |
*/ |
281 |
<T> T invokeAny(Collection<Callable<T>> tasks, |
282 |
long timeout, TimeUnit unit) |
283 |
throws InterruptedException, ExecutionException, TimeoutException; |
284 |
|
285 |
} |