ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/ExecutorService.java
Revision: 1.20
Committed: Sun Dec 21 12:24:48 2003 UTC (20 years, 5 months ago) by dl
Branch: MAIN
Changes since 1.19: +36 -14 lines
Log Message:
Documentation improvments; support two-arg submit

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.11 * @param tasks the collection of tasks
163 dl 1.13 * @return A list of Futures representing the tasks, in the same
164 dl 1.16 * sequential order as produced by the iterator for the given task list, each of which has
165 dl 1.13 * completed.
166 dl 1.11 * @throws InterruptedException if interrupted while waiting, in
167     * which case unfinished tasks are cancelled.
168 dl 1.13 * @throws NullPointerException if tasks or any of its elements are <tt>null</tt>
169     * @throws RejectedExecutionException if any task cannot be scheduled
170     * for execution
171 dl 1.11 */
172 dl 1.15
173 dl 1.16 <T> List<Future<T>> invokeAll(Collection<Callable<T>> tasks)
174 dl 1.11 throws InterruptedException;
175    
176     /**
177 dl 1.17 * Executes the given tasks, returning their results
178 dl 1.15 * when all complete or the timeout expires, whichever happens first.
179 dl 1.11 * Upon return, tasks that have not completed are cancelled.
180 dl 1.12 * Note that a <em>completed</em> task could have
181     * terminated either normally or by throwing an exception.
182 dl 1.11 * @param tasks the collection of tasks
183     * @param timeout the maximum time to wait
184 dl 1.15 * @param unit the time unit of the timeout argument
185 dl 1.13 * @return A list of Futures representing the tasks, in the same
186 dl 1.16 * sequential order as as produced by the iterator for the given task list. If the operation did
187 dl 1.13 * not time out, each task will have completed. If it did time
188 dl 1.15 * out, some of thiese tasks will not have completed.
189 dl 1.11 * @throws InterruptedException if interrupted while waiting, in
190     * which case unfinished tasks are cancelled.
191 dl 1.17 * @throws NullPointerException if tasks, any of its elements, or
192 dl 1.15 * unit are <tt>null</tt>
193 dl 1.13 * @throws RejectedExecutionException if any task cannot be scheduled
194     * for execution
195 dl 1.11 */
196 dl 1.16 <T> List<Future<T>> invokeAll(Collection<Callable<T>> tasks,
197 dl 1.18 long timeout, TimeUnit unit)
198 dl 1.11 throws InterruptedException;
199    
200     /**
201 dl 1.17 * Executes the given tasks, returning the result
202 dl 1.15 * of one that has completed successfully (i.e., without throwing
203     * an exception), if any do. Upon normal or exceptional return,
204     * tasks that have not completed are cancelled.
205 dl 1.11 * @param tasks the collection of tasks
206 dl 1.15 * @return The result returned by one of the tasks.
207     * @throws InterruptedException if interrupted while waiting
208     * @throws NullPointerException if tasks or any of its elements
209     * are <tt>null</tt>
210     * @throws IllegalArgumentException if tasks empty
211     * @throws ExecutionException if no task successfully completes
212     * @throws RejectedExecutionException if tasks cannot be scheduled
213 dl 1.13 * for execution
214 dl 1.11 */
215 dl 1.15 <T> T invokeAny(Collection<Callable<T>> tasks)
216     throws InterruptedException, ExecutionException;
217 dl 1.11
218     /**
219 dl 1.17 * Executes the given tasks, returning the result
220 dl 1.15 * of one that has completed successfully (i.e., without throwing
221     * an exception), if any do before the given timeout elapses.
222     * Upon normal or exceptional return, tasks that have not
223     * completed are cancelled.
224 dl 1.11 * @param tasks the collection of tasks
225     * @param timeout the maximum time to wait
226     * @param unit the time unit of the timeout argument
227 dl 1.15 * @return The result returned by one of the tasks.
228     * @throws InterruptedException if interrupted while waiting
229 dl 1.17 * @throws NullPointerException if tasks, any of its elements, or
230 dl 1.15 * unit are <tt>null</tt>
231     * @throws TimeoutException if the given timeout elapses before
232     * any task successfully completes
233     * @throws ExecutionException if no task successfully completes
234     * @throws RejectedExecutionException if tasks cannot be scheduled
235 dl 1.13 * for execution
236 dl 1.11 */
237 dl 1.15 <T> T invokeAny(Collection<Callable<T>> tasks,
238     long timeout, TimeUnit unit)
239     throws InterruptedException, ExecutionException, TimeoutException;
240 dl 1.3
241 dl 1.1 }