ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/AbstractExecutorService.java
(Generate patch)

Comparing jsr166/src/main/java/util/concurrent/AbstractExecutorService.java (file contents):
Revision 1.10 by dl, Fri Dec 19 14:42:25 2003 UTC vs.
Revision 1.11 by dl, Fri Dec 19 20:38:31 2003 UTC

# Line 29 | Line 29 | import java.util.concurrent.locks.*;
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);
32 >    public Future<?> submit(Runnable task) {
33 >        FutureTask<Object> ftask = new FutureTask<Object>(task, null);
34          execute(ftask);
35          return ftask;
36      }
# Line 41 | Line 41 | public abstract class AbstractExecutorSe
41          return ftask;
42      }
43  
44    public void invoke(Runnable task) throws ExecutionException, InterruptedException {
45        FutureTask<?> ftask = new FutureTask<Boolean>(task, null);
46        execute(ftask);
47        ftask.get();
48    }
49
44      public <T> T invoke(Callable<T> task) throws ExecutionException, InterruptedException {
45          FutureTask<T> ftask = new FutureTask<T>(task);
46          execute(ftask);
47          return ftask.get();
48      }
49  
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
50      // any/all methods, each a little bit different than the other
51  
52  
# Line 170 | Line 130 | public abstract class AbstractExecutorSe
130          }
131      }
132  
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
133      public <T> List<Future<T>> invokeAll(Collection<Callable<T>> tasks)
134          throws InterruptedException {
135          if (tasks == null)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines