ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/ExecutorService.java
Revision: 1.21
Committed: Mon Dec 22 16:25:20 2003 UTC (20 years, 5 months ago) by dl
Branch: MAIN
Changes since 1.20: +14 -5 lines
Log Message:
Simplify FutureTask and AbstractExecutorService internals; improve docs

File Contents

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