1 |
/* |
2 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
* Expert Group and released to the public domain, as explained at |
4 |
* http://creativecommons.org/licenses/publicdomain |
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 |
* The results of this method are undefined if the given |
163 |
* collection is modified while this operation is in progress. |
164 |
* @param tasks the collection of tasks |
165 |
* @return A list of Futures representing the tasks, in the same |
166 |
* sequential order as produced by the iterator for the given task |
167 |
* list, each of which has completed. |
168 |
* @throws InterruptedException if interrupted while waiting, in |
169 |
* which case unfinished tasks are cancelled. |
170 |
* @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 |
*/ |
174 |
|
175 |
<T> List<Future<T>> invokeAll(Collection<Callable<T>> tasks) |
176 |
throws InterruptedException; |
177 |
|
178 |
/** |
179 |
* Executes the given tasks, returning their results |
180 |
* when all complete or the timeout expires, whichever happens first. |
181 |
* Upon return, tasks that have not completed are cancelled. |
182 |
* Note that a <em>completed</em> task could have |
183 |
* terminated either normally or by throwing an exception. |
184 |
* The results of this method are undefined if the given |
185 |
* collection is modified while this operation is in progress. |
186 |
* @param tasks the collection of tasks |
187 |
* @param timeout the maximum time to wait |
188 |
* @param unit the time unit of the timeout argument |
189 |
* @return A list of Futures representing the tasks, in the same |
190 |
* 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 |
* @throws InterruptedException if interrupted while waiting, in |
195 |
* which case unfinished tasks are cancelled. |
196 |
* @throws NullPointerException if tasks, any of its elements, or |
197 |
* unit are <tt>null</tt> |
198 |
* @throws RejectedExecutionException if any task cannot be scheduled |
199 |
* for execution |
200 |
*/ |
201 |
<T> List<Future<T>> invokeAll(Collection<Callable<T>> tasks, |
202 |
long timeout, TimeUnit unit) |
203 |
throws InterruptedException; |
204 |
|
205 |
/** |
206 |
* Executes the given tasks, returning the result |
207 |
* 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 |
* The results of this method are undefined if the given |
211 |
* collection is modified while this operation is in progress. |
212 |
* @param tasks the collection of tasks |
213 |
* @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 |
* for execution |
221 |
*/ |
222 |
<T> T invokeAny(Collection<Callable<T>> tasks) |
223 |
throws InterruptedException, ExecutionException; |
224 |
|
225 |
/** |
226 |
* Executes the given tasks, returning the result |
227 |
* 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 |
* The results of this method are undefined if the given |
232 |
* collection is modified while this operation is in progress. |
233 |
* @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 |
* @return The result returned by one of the tasks. |
237 |
* @throws InterruptedException if interrupted while waiting |
238 |
* @throws NullPointerException if tasks, any of its elements, or |
239 |
* 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 |
* for execution |
245 |
*/ |
246 |
<T> T invokeAny(Collection<Callable<T>> tasks, |
247 |
long timeout, TimeUnit unit) |
248 |
throws InterruptedException, ExecutionException, TimeoutException; |
249 |
|
250 |
} |