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.10 |
* An <tt>Executor</tt> that provides methods to manage termination |
16 |
dl |
1.17 |
* and methods that can produce a {@link Future} for tracking |
17 |
dl |
1.11 |
* progress of one or more asynchronous tasks. |
18 |
dl |
1.17 |
* <p> |
19 |
|
|
* |
20 |
dl |
1.7 |
* 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 |
dl |
1.1 |
* executing, no tasks are awaiting execution, and no new tasks can be |
24 |
|
|
* submitted. |
25 |
|
|
* |
26 |
dl |
1.18 |
* <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 |
dl |
1.17 |
* versions of such methods.) |
34 |
|
|
* |
35 |
dl |
1.7 |
* <p>The {@link Executors} class provides factory methods for the |
36 |
|
|
* executor services provided in this package. |
37 |
dl |
1.1 |
* |
38 |
|
|
* @since 1.5 |
39 |
dl |
1.5 |
* @author Doug Lea |
40 |
dl |
1.1 |
*/ |
41 |
|
|
public interface ExecutorService extends Executor { |
42 |
tim |
1.8 |
|
43 |
dl |
1.17 |
|
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 |
tim |
1.8 |
/** |
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 |
dl |
1.18 |
* Submits a Runnable task for execution and returns a Future |
120 |
|
|
* representing that task. |
121 |
dl |
1.1 |
* |
122 |
dl |
1.17 |
* @param task the task to submit |
123 |
dl |
1.18 |
* @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 |
dl |
1.17 |
* @throws RejectedExecutionException if task cannot be scheduled |
127 |
|
|
* for execution |
128 |
dl |
1.1 |
*/ |
129 |
dl |
1.18 |
Future<?> submit(Runnable task); |
130 |
dl |
1.1 |
|
131 |
|
|
/** |
132 |
dl |
1.17 |
* Executes a value-returning task and blocks until it returns a |
133 |
|
|
* value or throws an exception. |
134 |
dl |
1.1 |
* |
135 |
dl |
1.17 |
* @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 |
dl |
1.1 |
*/ |
144 |
dl |
1.17 |
<T> T invoke(Callable<T> task) throws ExecutionException, InterruptedException; |
145 |
dl |
1.15 |
|
146 |
dl |
1.11 |
/** |
147 |
dl |
1.17 |
* Executes the given tasks, returning their results |
148 |
dl |
1.15 |
* when all complete. |
149 |
dl |
1.12 |
* Note that a <em>completed</em> task could have |
150 |
|
|
* terminated either normally or by throwing an exception. |
151 |
dl |
1.11 |
* @param tasks the collection of tasks |
152 |
dl |
1.13 |
* @return A list of Futures representing the tasks, in the same |
153 |
dl |
1.16 |
* sequential order as produced by the iterator for the given task list, each of which has |
154 |
dl |
1.13 |
* completed. |
155 |
dl |
1.11 |
* @throws InterruptedException if interrupted while waiting, in |
156 |
|
|
* which case unfinished tasks are cancelled. |
157 |
dl |
1.13 |
* @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 |
dl |
1.11 |
*/ |
161 |
dl |
1.15 |
|
162 |
dl |
1.16 |
<T> List<Future<T>> invokeAll(Collection<Callable<T>> tasks) |
163 |
dl |
1.11 |
throws InterruptedException; |
164 |
|
|
|
165 |
|
|
/** |
166 |
dl |
1.17 |
* Executes the given tasks, returning their results |
167 |
dl |
1.15 |
* when all complete or the timeout expires, whichever happens first. |
168 |
dl |
1.11 |
* Upon return, tasks that have not completed are cancelled. |
169 |
dl |
1.12 |
* Note that a <em>completed</em> task could have |
170 |
|
|
* terminated either normally or by throwing an exception. |
171 |
dl |
1.11 |
* @param tasks the collection of tasks |
172 |
|
|
* @param timeout the maximum time to wait |
173 |
dl |
1.15 |
* @param unit the time unit of the timeout argument |
174 |
dl |
1.13 |
* @return A list of Futures representing the tasks, in the same |
175 |
dl |
1.16 |
* sequential order as as produced by the iterator for the given task list. If the operation did |
176 |
dl |
1.13 |
* not time out, each task will have completed. If it did time |
177 |
dl |
1.15 |
* out, some of thiese tasks will not have completed. |
178 |
dl |
1.11 |
* @throws InterruptedException if interrupted while waiting, in |
179 |
|
|
* which case unfinished tasks are cancelled. |
180 |
dl |
1.17 |
* @throws NullPointerException if tasks, any of its elements, or |
181 |
dl |
1.15 |
* unit are <tt>null</tt> |
182 |
dl |
1.13 |
* @throws RejectedExecutionException if any task cannot be scheduled |
183 |
|
|
* for execution |
184 |
dl |
1.11 |
*/ |
185 |
dl |
1.16 |
<T> List<Future<T>> invokeAll(Collection<Callable<T>> tasks, |
186 |
dl |
1.18 |
long timeout, TimeUnit unit) |
187 |
dl |
1.11 |
throws InterruptedException; |
188 |
|
|
|
189 |
|
|
/** |
190 |
dl |
1.17 |
* Executes the given tasks, returning the result |
191 |
dl |
1.15 |
* 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 |
dl |
1.11 |
* @param tasks the collection of tasks |
195 |
dl |
1.15 |
* @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 |
dl |
1.13 |
* for execution |
203 |
dl |
1.11 |
*/ |
204 |
dl |
1.15 |
<T> T invokeAny(Collection<Callable<T>> tasks) |
205 |
|
|
throws InterruptedException, ExecutionException; |
206 |
dl |
1.11 |
|
207 |
|
|
/** |
208 |
dl |
1.17 |
* Executes the given tasks, returning the result |
209 |
dl |
1.15 |
* 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 |
dl |
1.11 |
* @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 |
dl |
1.15 |
* @return The result returned by one of the tasks. |
217 |
|
|
* @throws InterruptedException if interrupted while waiting |
218 |
dl |
1.17 |
* @throws NullPointerException if tasks, any of its elements, or |
219 |
dl |
1.15 |
* 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 |
dl |
1.13 |
* for execution |
225 |
dl |
1.11 |
*/ |
226 |
dl |
1.15 |
<T> T invokeAny(Collection<Callable<T>> tasks, |
227 |
|
|
long timeout, TimeUnit unit) |
228 |
|
|
throws InterruptedException, ExecutionException, TimeoutException; |
229 |
dl |
1.3 |
|
230 |
dl |
1.1 |
} |