ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/ExecutorService.java
Revision: 1.24
Committed: Sun Jan 11 23:19:55 2004 UTC (20 years, 4 months ago) by dl
Branch: MAIN
Changes since 1.23: +1 -1 lines
Log Message:
formatting; grammar

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    
9     import java.util.List;
10 dl 1.15 import java.util.Collection;
11 tim 1.8 import java.security.PrivilegedAction;
12     import java.security.PrivilegedExceptionAction;
13 dl 1.1
14     /**
15 dl 1.24 * An {@link Executor} that provides methods to manage termination, and
16 dl 1.20 * methods that can produce a {@link Future} for tracking progress of
17     * one or more asynchronous tasks. <p>
18 dl 1.17 *
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     * 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 dl 1.1 * @since 1.5
73 dl 1.5 * @author Doug Lea
74 dl 1.1 */
75     public interface ExecutorService extends Executor {
76 tim 1.8
77 dl 1.17 /**
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 tim 1.8 /**
141     * Submits a value-returning task for execution and returns a Future
142 dl 1.19 * representing the pending results of the task.
143 tim 1.8 *
144 dl 1.19 * <p>
145 dl 1.20 * If you would like to immediately block waiting
146 dl 1.19 * for a task, you can use constructions of the form
147     * <tt>result = exec.submit(aCallable).get();</tt>
148 dl 1.20 *
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 tim 1.8 * @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 dl 1.20 * @throws NullPointerException if task null
159 tim 1.8 */
160     <T> Future<T> submit(Callable<T> task);
161    
162     /**
163 dl 1.18 * Submits a Runnable task for execution and returns a Future
164 dl 1.20 * 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 dl 1.18 * representing that task.
181 dl 1.1 *
182 dl 1.17 * @param task the task to submit
183 dl 1.18 * @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 dl 1.17 * @throws RejectedExecutionException if task cannot be scheduled
187     * for execution
188 dl 1.20 * @throws NullPointerException if task null
189 dl 1.1 */
190 dl 1.18 Future<?> submit(Runnable task);
191 dl 1.15
192 dl 1.11 /**
193 dl 1.17 * Executes the given tasks, returning their results
194 dl 1.15 * when all complete.
195 dl 1.12 * Note that a <em>completed</em> task could have
196     * terminated either normally or by throwing an exception.
197 dl 1.21 * The results of this method are undefined if the given
198     * collection is modified while this operation is in progress.
199 dl 1.11 * @param tasks the collection of tasks
200 dl 1.13 * @return A list of Futures representing the tasks, in the same
201 dl 1.21 * sequential order as produced by the iterator for the given task
202     * list, each of which has completed.
203 dl 1.11 * @throws InterruptedException if interrupted while waiting, in
204     * which case unfinished tasks are cancelled.
205 dl 1.13 * @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 dl 1.11 */
209 dl 1.15
210 dl 1.16 <T> List<Future<T>> invokeAll(Collection<Callable<T>> tasks)
211 dl 1.11 throws InterruptedException;
212    
213     /**
214 dl 1.17 * Executes the given tasks, returning their results
215 dl 1.15 * when all complete or the timeout expires, whichever happens first.
216 dl 1.11 * Upon return, tasks that have not completed are cancelled.
217 dl 1.12 * Note that a <em>completed</em> task could have
218     * terminated either normally or by throwing an exception.
219 dl 1.21 * The results of this method are undefined if the given
220     * collection is modified while this operation is in progress.
221 dl 1.11 * @param tasks the collection of tasks
222     * @param timeout the maximum time to wait
223 dl 1.15 * @param unit the time unit of the timeout argument
224 dl 1.13 * @return A list of Futures representing the tasks, in the same
225 dl 1.21 * 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 dl 1.11 * @throws InterruptedException if interrupted while waiting, in
230     * which case unfinished tasks are cancelled.
231 dl 1.17 * @throws NullPointerException if tasks, any of its elements, or
232 dl 1.15 * unit are <tt>null</tt>
233 dl 1.13 * @throws RejectedExecutionException if any task cannot be scheduled
234     * for execution
235 dl 1.11 */
236 dl 1.16 <T> List<Future<T>> invokeAll(Collection<Callable<T>> tasks,
237 dl 1.18 long timeout, TimeUnit unit)
238 dl 1.11 throws InterruptedException;
239    
240     /**
241 dl 1.17 * Executes the given tasks, returning the result
242 dl 1.15 * 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 dl 1.21 * The results of this method are undefined if the given
246     * collection is modified while this operation is in progress.
247 dl 1.11 * @param tasks the collection of tasks
248 dl 1.15 * @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 dl 1.13 * for execution
256 dl 1.11 */
257 dl 1.15 <T> T invokeAny(Collection<Callable<T>> tasks)
258     throws InterruptedException, ExecutionException;
259 dl 1.11
260     /**
261 dl 1.17 * Executes the given tasks, returning the result
262 dl 1.15 * 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 dl 1.21 * The results of this method are undefined if the given
267     * collection is modified while this operation is in progress.
268 dl 1.11 * @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 dl 1.15 * @return The result returned by one of the tasks.
272     * @throws InterruptedException if interrupted while waiting
273 dl 1.17 * @throws NullPointerException if tasks, any of its elements, or
274 dl 1.15 * 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 dl 1.13 * for execution
280 dl 1.11 */
281 dl 1.15 <T> T invokeAny(Collection<Callable<T>> tasks,
282     long timeout, TimeUnit unit)
283     throws InterruptedException, ExecutionException, TimeoutException;
284 dl 1.3
285 dl 1.1 }