ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/ExecutorService.java
Revision: 1.38
Committed: Thu Aug 25 19:28:17 2005 UTC (18 years, 9 months ago) by jsr166
Branch: MAIN
Changes since 1.37: +5 -4 lines
Log Message:
6267833: Incorrect method signature ExecutorService.invokeAll()

File Contents

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