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 <tt>Executor</tt> that provides methods to manage termination |
16 |
* and methods that can produce a {@link Future} for tracking |
17 |
* progress of one or more asynchronous tasks. |
18 |
* <p> |
19 |
* |
20 |
* An <tt>ExecutorService</tt> can be shut down, which will cause it |
21 |
* to stop accepting new tasks. After being shut down, the executor |
22 |
* will eventually terminate, at which point no tasks are actively |
23 |
* executing, no tasks are awaiting execution, and no new tasks can be |
24 |
* submitted. |
25 |
* |
26 |
* <p> Method <tt>submit</tt> extends base method <tt>execute</tt> by |
27 |
* creating and returning a {@link Future} that can be used to cancel |
28 |
* execution and/or wait for completion. Methods <tt>invokeAny</tt> |
29 |
* and <tt>invokeAll</tt> perform the most commonly useful forms of |
30 |
* bulk execution, executing a collection of tasks and then waiting |
31 |
* for at least one, or all to complete. (Class {@link |
32 |
* ExecutorCompletionService} can be used to write customizable |
33 |
* versions of such methods.) |
34 |
* |
35 |
* <p>The {@link Executors} class provides factory methods for the |
36 |
* executor services provided in this package. |
37 |
* |
38 |
* @since 1.5 |
39 |
* @author Doug Lea |
40 |
*/ |
41 |
public interface ExecutorService extends Executor { |
42 |
|
43 |
|
44 |
/** |
45 |
* Initiates an orderly shutdown in which previously submitted |
46 |
* tasks are executed, but no new tasks will be |
47 |
* accepted. Invocation has no additional effect if already shut |
48 |
* down. |
49 |
* @throws SecurityException if a security manager exists and |
50 |
* shutting down this ExecutorService may manipulate threads that |
51 |
* the caller is not permitted to modify because it does not hold |
52 |
* {@link java.lang.RuntimePermission}<tt>("modifyThread")</tt>, |
53 |
* or the security manager's <tt>checkAccess</tt> method denies access. |
54 |
*/ |
55 |
void shutdown(); |
56 |
|
57 |
/** |
58 |
* Attempts to stop all actively executing tasks, halts the |
59 |
* processing of waiting tasks, and returns a list of the tasks that were |
60 |
* awaiting execution. |
61 |
* |
62 |
* <p>There are no guarantees beyond best-effort attempts to stop |
63 |
* processing actively executing tasks. For example, typical |
64 |
* implementations will cancel via {@link Thread#interrupt}, so if any |
65 |
* tasks mask or fail to respond to interrupts, they may never terminate. |
66 |
* |
67 |
* @return list of tasks that never commenced execution |
68 |
* @throws SecurityException if a security manager exists and |
69 |
* shutting down this ExecutorService may manipulate threads that |
70 |
* the caller is not permitted to modify because it does not hold |
71 |
* {@link java.lang.RuntimePermission}<tt>("modifyThread")</tt>, |
72 |
* or the security manager's <tt>checkAccess</tt> method denies access. |
73 |
*/ |
74 |
List<Runnable> shutdownNow(); |
75 |
|
76 |
/** |
77 |
* Returns <tt>true</tt> if this executor has been shut down. |
78 |
* |
79 |
* @return <tt>true</tt> if this executor has been shut down |
80 |
*/ |
81 |
boolean isShutdown(); |
82 |
|
83 |
/** |
84 |
* Returns <tt>true</tt> if all tasks have completed following shut down. |
85 |
* Note that <tt>isTerminated</tt> is never <tt>true</tt> unless |
86 |
* either <tt>shutdown</tt> or <tt>shutdownNow</tt> was called first. |
87 |
* |
88 |
* @return <tt>true</tt> if all tasks have completed following shut down |
89 |
*/ |
90 |
boolean isTerminated(); |
91 |
|
92 |
/** |
93 |
* Blocks until all tasks have completed execution after a shutdown |
94 |
* request, or the timeout occurs, or the current thread is |
95 |
* interrupted, whichever happens first. |
96 |
* |
97 |
* @param timeout the maximum time to wait |
98 |
* @param unit the time unit of the timeout argument |
99 |
* @return <tt>true</tt> if this executor terminated and <tt>false</tt> |
100 |
* if the timeout elapsed before termination |
101 |
* @throws InterruptedException if interrupted while waiting |
102 |
*/ |
103 |
boolean awaitTermination(long timeout, TimeUnit unit) |
104 |
throws InterruptedException; |
105 |
|
106 |
|
107 |
/** |
108 |
* Submits a value-returning task for execution and returns a Future |
109 |
* representing the pending results of the task. |
110 |
* |
111 |
* @param task the task to submit |
112 |
* @return a Future representing pending completion of the task |
113 |
* @throws RejectedExecutionException if task cannot be scheduled |
114 |
* for execution |
115 |
*/ |
116 |
<T> Future<T> submit(Callable<T> task); |
117 |
|
118 |
/** |
119 |
* Submits a Runnable task for execution and returns a Future |
120 |
* representing that task. |
121 |
* |
122 |
* @param task the task to submit |
123 |
* @return a Future representing pending completion of the task, |
124 |
* and whose <tt>get()</tt> method will return <tt>null</tt> |
125 |
* upon completion. |
126 |
* @throws RejectedExecutionException if task cannot be scheduled |
127 |
* for execution |
128 |
*/ |
129 |
Future<?> submit(Runnable task); |
130 |
|
131 |
/** |
132 |
* Executes a value-returning task and blocks until it returns a |
133 |
* value or throws an exception. |
134 |
* |
135 |
* @param task the task to submit |
136 |
* @return a Future representing pending completion of the task |
137 |
* @throws RejectedExecutionException if task cannot be scheduled |
138 |
* for execution |
139 |
* @throws InterruptedException if interrupted while waiting for |
140 |
* completion |
141 |
* @throws ExecutionException if the task encountered an exception |
142 |
* while executing |
143 |
*/ |
144 |
<T> T invoke(Callable<T> task) throws ExecutionException, InterruptedException; |
145 |
|
146 |
/** |
147 |
* Executes the given tasks, returning their results |
148 |
* when all complete. |
149 |
* Note that a <em>completed</em> task could have |
150 |
* terminated either normally or by throwing an exception. |
151 |
* @param tasks the collection of tasks |
152 |
* @return A list of Futures representing the tasks, in the same |
153 |
* sequential order as produced by the iterator for the given task list, each of which has |
154 |
* completed. |
155 |
* @throws InterruptedException if interrupted while waiting, in |
156 |
* which case unfinished tasks are cancelled. |
157 |
* @throws NullPointerException if tasks or any of its elements are <tt>null</tt> |
158 |
* @throws RejectedExecutionException if any task cannot be scheduled |
159 |
* for execution |
160 |
*/ |
161 |
|
162 |
<T> List<Future<T>> invokeAll(Collection<Callable<T>> tasks) |
163 |
throws InterruptedException; |
164 |
|
165 |
/** |
166 |
* Executes the given tasks, returning their results |
167 |
* when all complete or the timeout expires, whichever happens first. |
168 |
* Upon return, tasks that have not completed are cancelled. |
169 |
* Note that a <em>completed</em> task could have |
170 |
* terminated either normally or by throwing an exception. |
171 |
* @param tasks the collection of tasks |
172 |
* @param timeout the maximum time to wait |
173 |
* @param unit the time unit of the timeout argument |
174 |
* @return A list of Futures representing the tasks, in the same |
175 |
* sequential order as as produced by the iterator for the given task list. If the operation did |
176 |
* not time out, each task will have completed. If it did time |
177 |
* out, some of thiese tasks will not have completed. |
178 |
* @throws InterruptedException if interrupted while waiting, in |
179 |
* which case unfinished tasks are cancelled. |
180 |
* @throws NullPointerException if tasks, any of its elements, or |
181 |
* unit are <tt>null</tt> |
182 |
* @throws RejectedExecutionException if any task cannot be scheduled |
183 |
* for execution |
184 |
*/ |
185 |
<T> List<Future<T>> invokeAll(Collection<Callable<T>> tasks, |
186 |
long timeout, TimeUnit unit) |
187 |
throws InterruptedException; |
188 |
|
189 |
/** |
190 |
* Executes the given tasks, returning the result |
191 |
* of one that has completed successfully (i.e., without throwing |
192 |
* an exception), if any do. Upon normal or exceptional return, |
193 |
* tasks that have not completed are cancelled. |
194 |
* @param tasks the collection of tasks |
195 |
* @return The result returned by one of the tasks. |
196 |
* @throws InterruptedException if interrupted while waiting |
197 |
* @throws NullPointerException if tasks or any of its elements |
198 |
* are <tt>null</tt> |
199 |
* @throws IllegalArgumentException if tasks empty |
200 |
* @throws ExecutionException if no task successfully completes |
201 |
* @throws RejectedExecutionException if tasks cannot be scheduled |
202 |
* for execution |
203 |
*/ |
204 |
<T> T invokeAny(Collection<Callable<T>> tasks) |
205 |
throws InterruptedException, ExecutionException; |
206 |
|
207 |
/** |
208 |
* Executes the given tasks, returning the result |
209 |
* of one that has completed successfully (i.e., without throwing |
210 |
* an exception), if any do before the given timeout elapses. |
211 |
* Upon normal or exceptional return, tasks that have not |
212 |
* completed are cancelled. |
213 |
* @param tasks the collection of tasks |
214 |
* @param timeout the maximum time to wait |
215 |
* @param unit the time unit of the timeout argument |
216 |
* @return The result returned by one of the tasks. |
217 |
* @throws InterruptedException if interrupted while waiting |
218 |
* @throws NullPointerException if tasks, any of its elements, or |
219 |
* unit are <tt>null</tt> |
220 |
* @throws TimeoutException if the given timeout elapses before |
221 |
* any task successfully completes |
222 |
* @throws ExecutionException if no task successfully completes |
223 |
* @throws RejectedExecutionException if tasks cannot be scheduled |
224 |
* for execution |
225 |
*/ |
226 |
<T> T invokeAny(Collection<Callable<T>> tasks, |
227 |
long timeout, TimeUnit unit) |
228 |
throws InterruptedException, ExecutionException, TimeoutException; |
229 |
|
230 |
} |