1 |
tim |
1.1 |
/* |
2 |
|
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
dl |
1.17 |
* Expert Group and released to the public domain, as explained at |
4 |
jsr166 |
1.35 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
tim |
1.1 |
*/ |
6 |
|
|
|
7 |
|
|
package java.util.concurrent; |
8 |
jsr166 |
1.48 |
|
9 |
jsr166 |
1.49 |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
10 |
jsr166 |
1.50 |
|
11 |
jsr166 |
1.48 |
import java.util.ArrayList; |
12 |
|
|
import java.util.Collection; |
13 |
|
|
import java.util.Iterator; |
14 |
|
|
import java.util.List; |
15 |
tim |
1.1 |
|
16 |
|
|
/** |
17 |
jsr166 |
1.24 |
* Provides default implementations of {@link ExecutorService} |
18 |
jsr166 |
1.40 |
* execution methods. This class implements the {@code submit}, |
19 |
|
|
* {@code invokeAny} and {@code invokeAll} methods using a |
20 |
|
|
* {@link RunnableFuture} returned by {@code newTaskFor}, which defaults |
21 |
peierls |
1.21 |
* to the {@link FutureTask} class provided in this package. For example, |
22 |
jsr166 |
1.40 |
* the implementation of {@code submit(Runnable)} creates an |
23 |
|
|
* associated {@code RunnableFuture} that is executed and |
24 |
|
|
* returned. Subclasses may override the {@code newTaskFor} methods |
25 |
|
|
* to return {@code RunnableFuture} implementations other than |
26 |
|
|
* {@code FutureTask}. |
27 |
tim |
1.1 |
* |
28 |
jsr166 |
1.39 |
* <p><b>Extension example</b>. Here is a sketch of a class |
29 |
dl |
1.22 |
* that customizes {@link ThreadPoolExecutor} to use |
30 |
jsr166 |
1.40 |
* a {@code CustomTask} class instead of the default {@code FutureTask}: |
31 |
jsr166 |
1.51 |
* <pre> {@code |
32 |
dl |
1.22 |
* public class CustomThreadPoolExecutor extends ThreadPoolExecutor { |
33 |
|
|
* |
34 |
jsr166 |
1.32 |
* static class CustomTask<V> implements RunnableFuture<V> {...} |
35 |
dl |
1.22 |
* |
36 |
jsr166 |
1.32 |
* protected <V> RunnableFuture<V> newTaskFor(Callable<V> c) { |
37 |
|
|
* return new CustomTask<V>(c); |
38 |
jsr166 |
1.25 |
* } |
39 |
jsr166 |
1.32 |
* protected <V> RunnableFuture<V> newTaskFor(Runnable r, V v) { |
40 |
|
|
* return new CustomTask<V>(r, v); |
41 |
jsr166 |
1.25 |
* } |
42 |
|
|
* // ... add constructors, etc. |
43 |
jsr166 |
1.32 |
* }}</pre> |
44 |
|
|
* |
45 |
tim |
1.1 |
* @since 1.5 |
46 |
|
|
* @author Doug Lea |
47 |
|
|
*/ |
48 |
|
|
public abstract class AbstractExecutorService implements ExecutorService { |
49 |
|
|
|
50 |
peierls |
1.21 |
/** |
51 |
jsr166 |
1.40 |
* Returns a {@code RunnableFuture} for the given runnable and default |
52 |
peierls |
1.21 |
* value. |
53 |
jsr166 |
1.24 |
* |
54 |
peierls |
1.21 |
* @param runnable the runnable task being wrapped |
55 |
|
|
* @param value the default value for the returned future |
56 |
jsr166 |
1.46 |
* @param <T> the type of the given value |
57 |
jsr166 |
1.44 |
* @return a {@code RunnableFuture} which, when run, will run the |
58 |
jsr166 |
1.40 |
* underlying runnable and which, as a {@code Future}, will yield |
59 |
dl |
1.22 |
* the given value as its result and provide for cancellation of |
60 |
jsr166 |
1.43 |
* the underlying task |
61 |
dl |
1.22 |
* @since 1.6 |
62 |
peierls |
1.21 |
*/ |
63 |
|
|
protected <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) { |
64 |
|
|
return new FutureTask<T>(runnable, value); |
65 |
|
|
} |
66 |
|
|
|
67 |
|
|
/** |
68 |
jsr166 |
1.40 |
* Returns a {@code RunnableFuture} for the given callable task. |
69 |
jsr166 |
1.24 |
* |
70 |
peierls |
1.21 |
* @param callable the callable task being wrapped |
71 |
jsr166 |
1.46 |
* @param <T> the type of the callable's result |
72 |
jsr166 |
1.44 |
* @return a {@code RunnableFuture} which, when run, will call the |
73 |
jsr166 |
1.40 |
* underlying callable and which, as a {@code Future}, will yield |
74 |
dl |
1.22 |
* the callable's result as its result and provide for |
75 |
jsr166 |
1.43 |
* cancellation of the underlying task |
76 |
dl |
1.22 |
* @since 1.6 |
77 |
peierls |
1.21 |
*/ |
78 |
|
|
protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) { |
79 |
|
|
return new FutureTask<T>(callable); |
80 |
|
|
} |
81 |
|
|
|
82 |
jsr166 |
1.30 |
/** |
83 |
|
|
* @throws RejectedExecutionException {@inheritDoc} |
84 |
|
|
* @throws NullPointerException {@inheritDoc} |
85 |
|
|
*/ |
86 |
dl |
1.11 |
public Future<?> submit(Runnable task) { |
87 |
dl |
1.13 |
if (task == null) throw new NullPointerException(); |
88 |
jsr166 |
1.33 |
RunnableFuture<Void> ftask = newTaskFor(task, null); |
89 |
tim |
1.1 |
execute(ftask); |
90 |
|
|
return ftask; |
91 |
|
|
} |
92 |
|
|
|
93 |
jsr166 |
1.30 |
/** |
94 |
|
|
* @throws RejectedExecutionException {@inheritDoc} |
95 |
|
|
* @throws NullPointerException {@inheritDoc} |
96 |
|
|
*/ |
97 |
dl |
1.13 |
public <T> Future<T> submit(Runnable task, T result) { |
98 |
|
|
if (task == null) throw new NullPointerException(); |
99 |
peierls |
1.21 |
RunnableFuture<T> ftask = newTaskFor(task, result); |
100 |
dl |
1.13 |
execute(ftask); |
101 |
|
|
return ftask; |
102 |
|
|
} |
103 |
|
|
|
104 |
jsr166 |
1.30 |
/** |
105 |
|
|
* @throws RejectedExecutionException {@inheritDoc} |
106 |
|
|
* @throws NullPointerException {@inheritDoc} |
107 |
|
|
*/ |
108 |
tim |
1.1 |
public <T> Future<T> submit(Callable<T> task) { |
109 |
dl |
1.13 |
if (task == null) throw new NullPointerException(); |
110 |
peierls |
1.21 |
RunnableFuture<T> ftask = newTaskFor(task); |
111 |
tim |
1.1 |
execute(ftask); |
112 |
|
|
return ftask; |
113 |
|
|
} |
114 |
|
|
|
115 |
dl |
1.15 |
/** |
116 |
|
|
* the main mechanics of invokeAny. |
117 |
|
|
*/ |
118 |
jsr166 |
1.26 |
private <T> T doInvokeAny(Collection<? extends Callable<T>> tasks, |
119 |
jsr166 |
1.36 |
boolean timed, long nanos) |
120 |
dl |
1.15 |
throws InterruptedException, ExecutionException, TimeoutException { |
121 |
dl |
1.3 |
if (tasks == null) |
122 |
|
|
throw new NullPointerException(); |
123 |
dl |
1.15 |
int ntasks = tasks.size(); |
124 |
|
|
if (ntasks == 0) |
125 |
dl |
1.7 |
throw new IllegalArgumentException(); |
126 |
jsr166 |
1.47 |
ArrayList<Future<T>> futures = new ArrayList<>(ntasks); |
127 |
jsr166 |
1.20 |
ExecutorCompletionService<T> ecs = |
128 |
dl |
1.7 |
new ExecutorCompletionService<T>(this); |
129 |
dl |
1.15 |
|
130 |
|
|
// For efficiency, especially in executors with limited |
131 |
|
|
// parallelism, check to see if previously submitted tasks are |
132 |
dl |
1.18 |
// done before submitting more of them. This interleaving |
133 |
dl |
1.15 |
// plus the exception mechanics account for messiness of main |
134 |
dl |
1.18 |
// loop. |
135 |
dl |
1.15 |
|
136 |
dl |
1.3 |
try { |
137 |
dl |
1.15 |
// Record exceptions so that if we fail to obtain any |
138 |
|
|
// result, we can throw the last exception we got. |
139 |
dl |
1.7 |
ExecutionException ee = null; |
140 |
jsr166 |
1.37 |
final long deadline = timed ? System.nanoTime() + nanos : 0L; |
141 |
jsr166 |
1.26 |
Iterator<? extends Callable<T>> it = tasks.iterator(); |
142 |
dl |
1.15 |
|
143 |
|
|
// Start one task for sure; the rest incrementally |
144 |
|
|
futures.add(ecs.submit(it.next())); |
145 |
|
|
--ntasks; |
146 |
|
|
int active = 1; |
147 |
|
|
|
148 |
|
|
for (;;) { |
149 |
jsr166 |
1.20 |
Future<T> f = ecs.poll(); |
150 |
dl |
1.15 |
if (f == null) { |
151 |
|
|
if (ntasks > 0) { |
152 |
|
|
--ntasks; |
153 |
|
|
futures.add(ecs.submit(it.next())); |
154 |
|
|
++active; |
155 |
|
|
} |
156 |
jsr166 |
1.20 |
else if (active == 0) |
157 |
dl |
1.15 |
break; |
158 |
|
|
else if (timed) { |
159 |
jsr166 |
1.49 |
f = ecs.poll(nanos, NANOSECONDS); |
160 |
dl |
1.15 |
if (f == null) |
161 |
|
|
throw new TimeoutException(); |
162 |
jsr166 |
1.37 |
nanos = deadline - System.nanoTime(); |
163 |
dl |
1.15 |
} |
164 |
jsr166 |
1.20 |
else |
165 |
dl |
1.15 |
f = ecs.take(); |
166 |
|
|
} |
167 |
|
|
if (f != null) { |
168 |
|
|
--active; |
169 |
|
|
try { |
170 |
|
|
return f.get(); |
171 |
jsr166 |
1.19 |
} catch (ExecutionException eex) { |
172 |
dl |
1.15 |
ee = eex; |
173 |
jsr166 |
1.19 |
} catch (RuntimeException rex) { |
174 |
dl |
1.15 |
ee = new ExecutionException(rex); |
175 |
|
|
} |
176 |
dl |
1.7 |
} |
177 |
jsr166 |
1.20 |
} |
178 |
dl |
1.15 |
|
179 |
|
|
if (ee == null) |
180 |
|
|
ee = new ExecutionException(); |
181 |
|
|
throw ee; |
182 |
|
|
|
183 |
dl |
1.3 |
} finally { |
184 |
jsr166 |
1.49 |
cancelAll(futures); |
185 |
dl |
1.3 |
} |
186 |
|
|
} |
187 |
|
|
|
188 |
jsr166 |
1.26 |
public <T> T invokeAny(Collection<? extends Callable<T>> tasks) |
189 |
dl |
1.15 |
throws InterruptedException, ExecutionException { |
190 |
|
|
try { |
191 |
|
|
return doInvokeAny(tasks, false, 0); |
192 |
|
|
} catch (TimeoutException cannotHappen) { |
193 |
|
|
assert false; |
194 |
|
|
return null; |
195 |
|
|
} |
196 |
|
|
} |
197 |
|
|
|
198 |
jsr166 |
1.26 |
public <T> T invokeAny(Collection<? extends Callable<T>> tasks, |
199 |
jsr166 |
1.20 |
long timeout, TimeUnit unit) |
200 |
dl |
1.7 |
throws InterruptedException, ExecutionException, TimeoutException { |
201 |
dl |
1.15 |
return doInvokeAny(tasks, true, unit.toNanos(timeout)); |
202 |
dl |
1.3 |
} |
203 |
|
|
|
204 |
jsr166 |
1.26 |
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) |
205 |
dl |
1.3 |
throws InterruptedException { |
206 |
|
|
if (tasks == null) |
207 |
|
|
throw new NullPointerException(); |
208 |
jsr166 |
1.47 |
ArrayList<Future<T>> futures = new ArrayList<>(tasks.size()); |
209 |
dl |
1.3 |
try { |
210 |
|
|
for (Callable<T> t : tasks) { |
211 |
peierls |
1.21 |
RunnableFuture<T> f = newTaskFor(t); |
212 |
dl |
1.3 |
futures.add(f); |
213 |
|
|
execute(f); |
214 |
|
|
} |
215 |
jsr166 |
1.42 |
for (int i = 0, size = futures.size(); i < size; i++) { |
216 |
|
|
Future<T> f = futures.get(i); |
217 |
dl |
1.6 |
if (!f.isDone()) { |
218 |
jsr166 |
1.49 |
try { f.get(); } |
219 |
jsr166 |
1.52 |
catch (CancellationException | ExecutionException ignore) {} |
220 |
dl |
1.6 |
} |
221 |
|
|
} |
222 |
dl |
1.3 |
return futures; |
223 |
jsr166 |
1.49 |
} catch (Throwable t) { |
224 |
|
|
cancelAll(futures); |
225 |
|
|
throw t; |
226 |
dl |
1.3 |
} |
227 |
|
|
} |
228 |
|
|
|
229 |
jsr166 |
1.26 |
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, |
230 |
jsr166 |
1.20 |
long timeout, TimeUnit unit) |
231 |
dl |
1.3 |
throws InterruptedException { |
232 |
jsr166 |
1.38 |
if (tasks == null) |
233 |
dl |
1.3 |
throw new NullPointerException(); |
234 |
jsr166 |
1.49 |
final long nanos = unit.toNanos(timeout); |
235 |
|
|
final long deadline = System.nanoTime() + nanos; |
236 |
jsr166 |
1.47 |
ArrayList<Future<T>> futures = new ArrayList<>(tasks.size()); |
237 |
jsr166 |
1.49 |
int j = 0; |
238 |
|
|
timedOut: try { |
239 |
jsr166 |
1.20 |
for (Callable<T> t : tasks) |
240 |
peierls |
1.21 |
futures.add(newTaskFor(t)); |
241 |
dl |
1.16 |
|
242 |
jsr166 |
1.42 |
final int size = futures.size(); |
243 |
dl |
1.16 |
|
244 |
|
|
// Interleave time checks and calls to execute in case |
245 |
|
|
// executor doesn't have any/much parallelism. |
246 |
jsr166 |
1.42 |
for (int i = 0; i < size; i++) { |
247 |
jsr166 |
1.49 |
if (((i == 0) ? nanos : deadline - System.nanoTime()) <= 0L) |
248 |
|
|
break timedOut; |
249 |
jsr166 |
1.42 |
execute((Runnable)futures.get(i)); |
250 |
dl |
1.3 |
} |
251 |
dl |
1.16 |
|
252 |
jsr166 |
1.49 |
for (; j < size; j++) { |
253 |
|
|
Future<T> f = futures.get(j); |
254 |
dl |
1.6 |
if (!f.isDone()) { |
255 |
jsr166 |
1.49 |
try { f.get(deadline - System.nanoTime(), NANOSECONDS); } |
256 |
jsr166 |
1.52 |
catch (CancellationException | ExecutionException ignore) {} |
257 |
jsr166 |
1.49 |
catch (TimeoutException timedOut) { |
258 |
|
|
break timedOut; |
259 |
dl |
1.6 |
} |
260 |
|
|
} |
261 |
|
|
} |
262 |
dl |
1.3 |
return futures; |
263 |
jsr166 |
1.49 |
} catch (Throwable t) { |
264 |
|
|
cancelAll(futures); |
265 |
|
|
throw t; |
266 |
dl |
1.3 |
} |
267 |
jsr166 |
1.49 |
// Timed out before all the tasks could be completed; cancel remaining |
268 |
|
|
cancelAll(futures, j); |
269 |
|
|
return futures; |
270 |
dl |
1.3 |
} |
271 |
|
|
|
272 |
jsr166 |
1.49 |
private static <T> void cancelAll(ArrayList<Future<T>> futures) { |
273 |
|
|
cancelAll(futures, 0); |
274 |
|
|
} |
275 |
|
|
|
276 |
|
|
/** Cancels all futures with index at least j. */ |
277 |
|
|
private static <T> void cancelAll(ArrayList<Future<T>> futures, int j) { |
278 |
|
|
for (int size = futures.size(); j < size; j++) |
279 |
|
|
futures.get(j).cancel(true); |
280 |
|
|
} |
281 |
tim |
1.1 |
} |