ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/AbstractExecutorService.java
Revision: 1.9
Committed: Wed Dec 17 17:00:24 2003 UTC (20 years, 5 months ago) by dl
Branch: MAIN
Changes since 1.8: +4 -4 lines
Log Message:
Export delegation wrappers; fix/add documentation

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.security.AccessControlContext;
10 import java.security.AccessController;
11 import java.security.PrivilegedAction;
12 import java.security.PrivilegedExceptionAction;
13 import java.util.*;
14 import java.util.concurrent.locks.*;
15
16 /**
17 * Provides default implementation of {@link ExecutorService}
18 * execution methods. This class implements the <tt>submit</tt> and
19 * <tt>invoke</tt> methods using the default {@link FutureTask} and
20 * {@link PrivilegedFutureTask} classes provided in this package. For
21 * example, the the implementation of <tt>submit(Runnable)</tt>
22 * creates an associated <tt>FutureTask</tt> that is executed and
23 * returned. Subclasses overriding these methods to use different
24 * {@link Future} implementations should do so consistently for each
25 * of these methods.
26 *
27 * @since 1.5
28 * @author Doug Lea
29 */
30 public abstract class AbstractExecutorService implements ExecutorService {
31
32 public <T> Future<T> submit(Runnable task, T result) {
33 FutureTask<T> ftask = new FutureTask<T>(task, result);
34 execute(ftask);
35 return ftask;
36 }
37
38 public <T> Future<T> submit(Callable<T> task) {
39 FutureTask<T> ftask = new FutureTask<T>(task);
40 execute(ftask);
41 return ftask;
42 }
43
44 public void invoke(Runnable task) throws ExecutionException, InterruptedException {
45 FutureTask<?> ftask = new FutureTask<Boolean>(task, Boolean.TRUE);
46 execute(ftask);
47 ftask.get();
48 }
49
50 public <T> T invoke(Callable<T> task) throws ExecutionException, InterruptedException {
51 FutureTask<T> ftask = new FutureTask<T>(task);
52 execute(ftask);
53 return ftask.get();
54 }
55
56 public Future<Object> submit(PrivilegedAction action) {
57 Callable<Object> task = new PrivilegedActionAdapter(action);
58 FutureTask<Object> future = new PrivilegedFutureTask<Object>(task);
59 execute(future);
60 return future;
61 }
62
63 public Future<Object> submit(PrivilegedExceptionAction action) {
64 Callable<Object> task = new PrivilegedExceptionActionAdapter(action);
65 FutureTask<Object> future = new PrivilegedFutureTask<Object>(task);
66 execute(future);
67 return future;
68 }
69
70 private static class PrivilegedActionAdapter implements Callable<Object> {
71 PrivilegedActionAdapter(PrivilegedAction action) {
72 this.action = action;
73 }
74 public Object call () {
75 return action.run();
76 }
77 private final PrivilegedAction action;
78 }
79
80 private static class PrivilegedExceptionActionAdapter implements Callable<Object> {
81 PrivilegedExceptionActionAdapter(PrivilegedExceptionAction action) {
82 this.action = action;
83 }
84 public Object call () throws Exception {
85 return action.run();
86 }
87 private final PrivilegedExceptionAction action;
88 }
89
90 // any/all methods, each a little bit different than the other
91
92
93 public <T> T invokeAny(Collection<Callable<T>> tasks)
94 throws InterruptedException, ExecutionException {
95 if (tasks == null)
96 throw new NullPointerException();
97 int n = tasks.size();
98 if (n == 0)
99 throw new IllegalArgumentException();
100 List<Future<T>> futures= new ArrayList<Future<T>>(n);
101 ExecutorCompletionService<T> ecs =
102 new ExecutorCompletionService<T>(this);
103 try {
104 for (Callable<T> t : tasks)
105 futures.add(ecs.submit(t));
106 ExecutionException ee = null;
107 RuntimeException re = null;
108 while (n-- > 0) {
109 Future<T> f = ecs.take();
110 try {
111 return f.get();
112 } catch(ExecutionException eex) {
113 ee = eex;
114 } catch(RuntimeException rex) {
115 re = rex;
116 }
117 }
118 if (ee != null)
119 throw ee;
120 if (re != null)
121 throw new ExecutionException(re);
122 throw new ExecutionException();
123 } finally {
124 for (Future<T> f : futures)
125 f.cancel(true);
126 }
127 }
128
129 public <T> T invokeAny(Collection<Callable<T>> tasks,
130 long timeout, TimeUnit unit)
131 throws InterruptedException, ExecutionException, TimeoutException {
132 if (tasks == null || unit == null)
133 throw new NullPointerException();
134 long nanos = unit.toNanos(timeout);
135 int n = tasks.size();
136 if (n == 0)
137 throw new IllegalArgumentException();
138 List<Future<T>> futures= new ArrayList<Future<T>>(n);
139 ExecutorCompletionService<T> ecs =
140 new ExecutorCompletionService<T>(this);
141 try {
142 for (Callable<T> t : tasks)
143 futures.add(ecs.submit(t));
144 ExecutionException ee = null;
145 RuntimeException re = null;
146 long lastTime = System.nanoTime();
147 while (n-- > 0) {
148 Future<T> f = ecs.poll(nanos, TimeUnit.NANOSECONDS);
149 if (f == null)
150 throw new TimeoutException();
151 try {
152 return f.get();
153 } catch(ExecutionException eex) {
154 ee = eex;
155 } catch(RuntimeException rex) {
156 re = rex;
157 }
158 long now = System.nanoTime();
159 nanos -= now - lastTime;
160 lastTime = now;
161 }
162 if (ee != null)
163 throw ee;
164 if (re != null)
165 throw new ExecutionException(re);
166 throw new ExecutionException();
167 } finally {
168 for (Future<T> f : futures)
169 f.cancel(true);
170 }
171 }
172
173
174 public <T> T invokeAny(Collection<Runnable> tasks, T result)
175 throws InterruptedException, ExecutionException {
176 if (tasks == null)
177 throw new NullPointerException();
178 int n = tasks.size();
179 if (n == 0)
180 throw new IllegalArgumentException();
181 List<Future<T>> futures= new ArrayList<Future<T>>(n);
182 ExecutorCompletionService<T> ecs =
183 new ExecutorCompletionService<T>(this);
184 try {
185 for (Runnable t : tasks)
186 futures.add(ecs.submit(t, result));
187 ExecutionException ee = null;
188 RuntimeException re = null;
189 while (n-- > 0) {
190 Future<T> f = ecs.take();
191 try {
192 return f.get();
193 } catch(ExecutionException eex) {
194 ee = eex;
195 } catch(RuntimeException rex) {
196 re = rex;
197 }
198 }
199 if (ee != null)
200 throw ee;
201 if (re != null)
202 throw new ExecutionException(re);
203 throw new ExecutionException();
204 } finally {
205 for (Future<T> f : futures)
206 f.cancel(true);
207 }
208 }
209
210 public <T> T invokeAny(Collection<Runnable> tasks, T result,
211 long timeout, TimeUnit unit)
212 throws InterruptedException, ExecutionException, TimeoutException {
213 if (tasks == null || unit == null)
214 throw new NullPointerException();
215 long nanos = unit.toNanos(timeout);
216 int n = tasks.size();
217 if (n == 0)
218 throw new IllegalArgumentException();
219 List<Future<T>> futures= new ArrayList<Future<T>>(n);
220 ExecutorCompletionService<T> ecs =
221 new ExecutorCompletionService<T>(this);
222 try {
223 for (Runnable t : tasks)
224 futures.add(ecs.submit(t, result));
225 ExecutionException ee = null;
226 RuntimeException re = null;
227 long lastTime = System.nanoTime();
228 while (n-- > 0) {
229 Future<T> f = ecs.poll(nanos, TimeUnit.NANOSECONDS);
230 if (f == null)
231 throw new TimeoutException();
232 try {
233 return f.get();
234 } catch(ExecutionException eex) {
235 ee = eex;
236 } catch(RuntimeException rex) {
237 re = rex;
238 }
239 long now = System.nanoTime();
240 nanos -= now - lastTime;
241 lastTime = now;
242 }
243 if (ee != null)
244 throw ee;
245 if (re != null)
246 throw new ExecutionException(re);
247 throw new ExecutionException();
248 } finally {
249 for (Future<T> f : futures)
250 f.cancel(true);
251 }
252 }
253
254
255 public <T> List<Future<T>> invokeAll(Collection<Runnable> tasks, T result)
256 throws InterruptedException {
257 if (tasks == null)
258 throw new NullPointerException();
259 List<Future<T>> futures = new ArrayList<Future<T>>(tasks.size());
260 boolean done = false;
261 try {
262 for (Runnable t : tasks) {
263 FutureTask<T> f = new FutureTask<T>(t, result);
264 futures.add(f);
265 execute(f);
266 }
267 for (Future<T> f : futures) {
268 if (!f.isDone()) {
269 try {
270 f.get();
271 } catch(CancellationException ignore) {
272 } catch(ExecutionException ignore) {
273 }
274 }
275 }
276 done = true;
277 return futures;
278 } finally {
279 if (!done)
280 for (Future<T> f : futures)
281 f.cancel(true);
282 }
283 }
284
285 public <T> List<Future<T>> invokeAll(Collection<Runnable> tasks, T result,
286 long timeout, TimeUnit unit)
287 throws InterruptedException {
288 if (tasks == null || unit == null)
289 throw new NullPointerException();
290 long nanos = unit.toNanos(timeout);
291 List<Future<T>> futures = new ArrayList<Future<T>>(tasks.size());
292 boolean done = false;
293 try {
294 for (Runnable t : tasks) {
295 FutureTask<T> f = new FutureTask<T>(t, result);
296 futures.add(f);
297 execute(f);
298 }
299 long lastTime = System.nanoTime();
300 for (Future<T> f : futures) {
301 if (!f.isDone()) {
302 if (nanos < 0)
303 return futures;
304 try {
305 f.get(nanos, TimeUnit.NANOSECONDS);
306 } catch(CancellationException ignore) {
307 } catch(ExecutionException ignore) {
308 } catch(TimeoutException toe) {
309 return futures;
310 }
311 long now = System.nanoTime();
312 nanos -= now - lastTime;
313 lastTime = now;
314 }
315 }
316 done = true;
317 return futures;
318 } finally {
319 if (!done)
320 for (Future<T> f : futures)
321 f.cancel(true);
322 }
323 }
324
325 public <T> List<Future<T>> invokeAll(Collection<Callable<T>> tasks)
326 throws InterruptedException {
327 if (tasks == null)
328 throw new NullPointerException();
329 List<Future<T>> futures = new ArrayList<Future<T>>(tasks.size());
330 boolean done = false;
331 try {
332 for (Callable<T> t : tasks) {
333 FutureTask<T> f = new FutureTask<T>(t);
334 futures.add(f);
335 execute(f);
336 }
337 for (Future<T> f : futures) {
338 if (!f.isDone()) {
339 try {
340 f.get();
341 } catch(CancellationException ignore) {
342 } catch(ExecutionException ignore) {
343 }
344 }
345 }
346 done = true;
347 return futures;
348 } finally {
349 if (!done)
350 for (Future<T> f : futures)
351 f.cancel(true);
352 }
353 }
354
355 public <T> List<Future<T>> invokeAll(Collection<Callable<T>> tasks,
356 long timeout, TimeUnit unit)
357 throws InterruptedException {
358 if (tasks == null || unit == null)
359 throw new NullPointerException();
360 long nanos = unit.toNanos(timeout);
361 List<Future<T>> futures = new ArrayList<Future<T>>(tasks.size());
362 boolean done = false;
363 try {
364 for (Callable<T> t : tasks) {
365 FutureTask<T> f = new FutureTask<T>(t);
366 futures.add(f);
367 execute(f);
368 }
369 long lastTime = System.nanoTime();
370 for (Future<T> f : futures) {
371 if (!f.isDone()) {
372 if (nanos < 0)
373 return futures;
374 try {
375 f.get(nanos, TimeUnit.NANOSECONDS);
376 } catch(CancellationException ignore) {
377 } catch(ExecutionException ignore) {
378 } catch(TimeoutException toe) {
379 return futures;
380 }
381 long now = System.nanoTime();
382 nanos -= now - lastTime;
383 lastTime = now;
384 }
385 }
386 done = true;
387 return futures;
388 } finally {
389 if (!done)
390 for (Future<T> f : futures)
391 f.cancel(true);
392 }
393 }
394
395 }