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

# Content
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 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. <p>
18 *
19 * 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 * executing, no tasks are awaiting execution, and no new tasks can be
23 * submitted.
24 *
25 * <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 *
34 * <p>The {@link Executors} class provides factory methods for the
35 * executor services provided in this package.
36 *
37 * @since 1.5
38 * @author Doug Lea
39 */
40 public interface ExecutorService extends Executor {
41
42 /**
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 /**
106 * Submits a value-returning task for execution and returns a Future
107 * representing the pending results of the task.
108 *
109 * <p>
110 * If you would like to immediately block waiting
111 * for a task, you can use constructions of the form
112 * <tt>result = exec.submit(aCallable).get();</tt>
113 *
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 * @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 * @throws NullPointerException if task null
124 */
125 <T> Future<T> submit(Callable<T> task);
126
127 /**
128 * Submits a Runnable task for execution and returns a Future
129 * 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 * representing that task.
146 *
147 * @param task the task to submit
148 * @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 * @throws RejectedExecutionException if task cannot be scheduled
152 * for execution
153 * @throws NullPointerException if task null
154 */
155 Future<?> submit(Runnable task);
156
157 /**
158 * Executes the given tasks, returning their results
159 * when all complete.
160 * Note that a <em>completed</em> task could have
161 * terminated either normally or by throwing an exception.
162 * @param tasks the collection of tasks
163 * @return A list of Futures representing the tasks, in the same
164 * sequential order as produced by the iterator for the given task list, each of which has
165 * completed.
166 * @throws InterruptedException if interrupted while waiting, in
167 * which case unfinished tasks are cancelled.
168 * @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 */
172
173 <T> List<Future<T>> invokeAll(Collection<Callable<T>> tasks)
174 throws InterruptedException;
175
176 /**
177 * Executes the given tasks, returning their results
178 * when all complete or the timeout expires, whichever happens first.
179 * Upon return, tasks that have not completed are cancelled.
180 * Note that a <em>completed</em> task could have
181 * terminated either normally or by throwing an exception.
182 * @param tasks the collection of tasks
183 * @param timeout the maximum time to wait
184 * @param unit the time unit of the timeout argument
185 * @return A list of Futures representing the tasks, in the same
186 * sequential order as as produced by the iterator for the given task list. If the operation did
187 * not time out, each task will have completed. If it did time
188 * out, some of thiese tasks will not have completed.
189 * @throws InterruptedException if interrupted while waiting, in
190 * which case unfinished tasks are cancelled.
191 * @throws NullPointerException if tasks, any of its elements, or
192 * unit are <tt>null</tt>
193 * @throws RejectedExecutionException if any task cannot be scheduled
194 * for execution
195 */
196 <T> List<Future<T>> invokeAll(Collection<Callable<T>> tasks,
197 long timeout, TimeUnit unit)
198 throws InterruptedException;
199
200 /**
201 * Executes the given tasks, returning the result
202 * 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 * @param tasks the collection of tasks
206 * @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 * for execution
214 */
215 <T> T invokeAny(Collection<Callable<T>> tasks)
216 throws InterruptedException, ExecutionException;
217
218 /**
219 * Executes the given tasks, returning the result
220 * 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 * @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 * @return The result returned by one of the tasks.
228 * @throws InterruptedException if interrupted while waiting
229 * @throws NullPointerException if tasks, any of its elements, or
230 * 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 * for execution
236 */
237 <T> T invokeAny(Collection<Callable<T>> tasks,
238 long timeout, TimeUnit unit)
239 throws InterruptedException, ExecutionException, TimeoutException;
240
241 }