ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/AbstractExecutorService.java
Revision: 1.44
Committed: Tue Feb 5 20:09:33 2013 UTC (11 years, 3 months ago) by jsr166
Branch: MAIN
Changes since 1.43: +2 -2 lines
Log Message:
give that javadoc some commas

File Contents

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