ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/AbstractExecutorService.java
Revision: 1.12
Committed: Sat Dec 20 14:00:05 2003 UTC (20 years, 5 months ago) by dl
Branch: MAIN
Changes since 1.11: +5 -16 lines
Log Message:
Replace PrivilegedFutureTask with Executors.privilegedCallable

File Contents

# User Rev Content
1 tim 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 dl 1.3 import java.util.*;
10 tim 1.1
11     /**
12 dl 1.2 * Provides default implementation of {@link ExecutorService}
13 dl 1.12 * execution methods. This class implements the <tt>submit</tt>,
14     * <tt>invokeAny</tt> and <tt>invokeAll</tt> methods using the default
15     * {@link FutureTask} class provided in this package. For example,
16     * the the implementation of <tt>submit(Runnable)</tt> creates an
17     * associated <tt>FutureTask</tt> that is executed and
18 dl 1.2 * returned. Subclasses overriding these methods to use different
19     * {@link Future} implementations should do so consistently for each
20     * of these methods.
21 tim 1.1 *
22     * @since 1.5
23     * @author Doug Lea
24     */
25     public abstract class AbstractExecutorService implements ExecutorService {
26    
27 dl 1.11 public Future<?> submit(Runnable task) {
28     FutureTask<Object> ftask = new FutureTask<Object>(task, null);
29 tim 1.1 execute(ftask);
30     return ftask;
31     }
32    
33     public <T> Future<T> submit(Callable<T> task) {
34     FutureTask<T> ftask = new FutureTask<T>(task);
35     execute(ftask);
36     return ftask;
37     }
38    
39 dl 1.7 // any/all methods, each a little bit different than the other
40 dl 1.3
41    
42 dl 1.7 public <T> T invokeAny(Collection<Callable<T>> tasks)
43     throws InterruptedException, ExecutionException {
44 dl 1.3 if (tasks == null)
45     throw new NullPointerException();
46     int n = tasks.size();
47     if (n == 0)
48 dl 1.7 throw new IllegalArgumentException();
49     List<Future<T>> futures= new ArrayList<Future<T>>(n);
50     ExecutorCompletionService<T> ecs =
51     new ExecutorCompletionService<T>(this);
52 dl 1.3 try {
53 dl 1.7 for (Callable<T> t : tasks)
54     futures.add(ecs.submit(t));
55     ExecutionException ee = null;
56     RuntimeException re = null;
57     while (n-- > 0) {
58     Future<T> f = ecs.take();
59     try {
60     return f.get();
61     } catch(ExecutionException eex) {
62     ee = eex;
63     } catch(RuntimeException rex) {
64     re = rex;
65     }
66     }
67     if (ee != null)
68     throw ee;
69     if (re != null)
70     throw new ExecutionException(re);
71     throw new ExecutionException();
72 dl 1.3 } finally {
73 dl 1.6 for (Future<T> f : futures)
74 dl 1.4 f.cancel(true);
75 dl 1.3 }
76     }
77    
78 dl 1.7 public <T> T invokeAny(Collection<Callable<T>> tasks,
79     long timeout, TimeUnit unit)
80     throws InterruptedException, ExecutionException, TimeoutException {
81 dl 1.3 if (tasks == null || unit == null)
82     throw new NullPointerException();
83 dl 1.7 long nanos = unit.toNanos(timeout);
84 dl 1.3 int n = tasks.size();
85     if (n == 0)
86 dl 1.7 throw new IllegalArgumentException();
87     List<Future<T>> futures= new ArrayList<Future<T>>(n);
88     ExecutorCompletionService<T> ecs =
89     new ExecutorCompletionService<T>(this);
90 dl 1.3 try {
91 dl 1.7 for (Callable<T> t : tasks)
92     futures.add(ecs.submit(t));
93     ExecutionException ee = null;
94     RuntimeException re = null;
95     long lastTime = System.nanoTime();
96     while (n-- > 0) {
97     Future<T> f = ecs.poll(nanos, TimeUnit.NANOSECONDS);
98 dl 1.8 if (f == null)
99     throw new TimeoutException();
100 dl 1.7 try {
101     return f.get();
102     } catch(ExecutionException eex) {
103     ee = eex;
104     } catch(RuntimeException rex) {
105     re = rex;
106     }
107 dl 1.8 long now = System.nanoTime();
108     nanos -= now - lastTime;
109     lastTime = now;
110 dl 1.7 }
111     if (ee != null)
112     throw ee;
113     if (re != null)
114     throw new ExecutionException(re);
115     throw new ExecutionException();
116 dl 1.3 } finally {
117 dl 1.6 for (Future<T> f : futures)
118 dl 1.4 f.cancel(true);
119 dl 1.3 }
120     }
121    
122 dl 1.9 public <T> List<Future<T>> invokeAll(Collection<Callable<T>> tasks)
123 dl 1.3 throws InterruptedException {
124     if (tasks == null)
125     throw new NullPointerException();
126 dl 1.6 List<Future<T>> futures = new ArrayList<Future<T>>(tasks.size());
127     boolean done = false;
128 dl 1.3 try {
129     for (Callable<T> t : tasks) {
130 dl 1.6 FutureTask<T> f = new FutureTask<T>(t);
131 dl 1.3 futures.add(f);
132     execute(f);
133     }
134 dl 1.6 for (Future<T> f : futures) {
135     if (!f.isDone()) {
136     try {
137     f.get();
138     } catch(CancellationException ignore) {
139     } catch(ExecutionException ignore) {
140     }
141     }
142     }
143     done = true;
144 dl 1.3 return futures;
145     } finally {
146 dl 1.6 if (!done)
147 dl 1.4 for (Future<T> f : futures)
148     f.cancel(true);
149 dl 1.3 }
150     }
151    
152 dl 1.9 public <T> List<Future<T>> invokeAll(Collection<Callable<T>> tasks,
153 dl 1.6 long timeout, TimeUnit unit)
154 dl 1.3 throws InterruptedException {
155     if (tasks == null || unit == null)
156     throw new NullPointerException();
157     long nanos = unit.toNanos(timeout);
158 dl 1.6 List<Future<T>> futures = new ArrayList<Future<T>>(tasks.size());
159     boolean done = false;
160 dl 1.3 try {
161     for (Callable<T> t : tasks) {
162 dl 1.6 FutureTask<T> f = new FutureTask<T>(t);
163 dl 1.3 futures.add(f);
164     execute(f);
165     }
166 dl 1.6 long lastTime = System.nanoTime();
167     for (Future<T> f : futures) {
168     if (!f.isDone()) {
169     if (nanos < 0)
170     return futures;
171     try {
172     f.get(nanos, TimeUnit.NANOSECONDS);
173     } catch(CancellationException ignore) {
174     } catch(ExecutionException ignore) {
175     } catch(TimeoutException toe) {
176     return futures;
177     }
178 dl 1.8 long now = System.nanoTime();
179     nanos -= now - lastTime;
180     lastTime = now;
181 dl 1.6 }
182     }
183     done = true;
184 dl 1.3 return futures;
185     } finally {
186 dl 1.6 if (!done)
187 dl 1.4 for (Future<T> f : futures)
188     f.cancel(true);
189 dl 1.3 }
190     }
191    
192 tim 1.1 }