ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/ExecutorService.java
Revision: 1.44
Committed: Tue Feb 7 20:54:24 2006 UTC (18 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.43: +0 -1 lines
Log Message:
6378729: Remove workaround for 6280605

File Contents

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