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.util.*; |
10 |
|
11 |
/** |
12 |
* Provides default implementation of {@link ExecutorService} |
13 |
* 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 |
* returned. Subclasses overriding these methods to use different |
19 |
* {@link Future} implementations should do so consistently for each |
20 |
* of these methods. |
21 |
* |
22 |
* @since 1.5 |
23 |
* @author Doug Lea |
24 |
*/ |
25 |
public abstract class AbstractExecutorService implements ExecutorService { |
26 |
|
27 |
public Future<?> submit(Runnable task) { |
28 |
FutureTask<Object> ftask = new FutureTask<Object>(task, null); |
29 |
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 |
// any/all methods, each a little bit different than the other |
40 |
|
41 |
|
42 |
public <T> T invokeAny(Collection<Callable<T>> tasks) |
43 |
throws InterruptedException, ExecutionException { |
44 |
if (tasks == null) |
45 |
throw new NullPointerException(); |
46 |
int n = tasks.size(); |
47 |
if (n == 0) |
48 |
throw new IllegalArgumentException(); |
49 |
List<Future<T>> futures= new ArrayList<Future<T>>(n); |
50 |
ExecutorCompletionService<T> ecs = |
51 |
new ExecutorCompletionService<T>(this); |
52 |
try { |
53 |
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 |
} finally { |
73 |
for (Future<T> f : futures) |
74 |
f.cancel(true); |
75 |
} |
76 |
} |
77 |
|
78 |
public <T> T invokeAny(Collection<Callable<T>> tasks, |
79 |
long timeout, TimeUnit unit) |
80 |
throws InterruptedException, ExecutionException, TimeoutException { |
81 |
if (tasks == null || unit == null) |
82 |
throw new NullPointerException(); |
83 |
long nanos = unit.toNanos(timeout); |
84 |
int n = tasks.size(); |
85 |
if (n == 0) |
86 |
throw new IllegalArgumentException(); |
87 |
List<Future<T>> futures= new ArrayList<Future<T>>(n); |
88 |
ExecutorCompletionService<T> ecs = |
89 |
new ExecutorCompletionService<T>(this); |
90 |
try { |
91 |
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 |
if (f == null) |
99 |
throw new TimeoutException(); |
100 |
try { |
101 |
return f.get(); |
102 |
} catch(ExecutionException eex) { |
103 |
ee = eex; |
104 |
} catch(RuntimeException rex) { |
105 |
re = rex; |
106 |
} |
107 |
long now = System.nanoTime(); |
108 |
nanos -= now - lastTime; |
109 |
lastTime = now; |
110 |
} |
111 |
if (ee != null) |
112 |
throw ee; |
113 |
if (re != null) |
114 |
throw new ExecutionException(re); |
115 |
throw new ExecutionException(); |
116 |
} finally { |
117 |
for (Future<T> f : futures) |
118 |
f.cancel(true); |
119 |
} |
120 |
} |
121 |
|
122 |
public <T> List<Future<T>> invokeAll(Collection<Callable<T>> tasks) |
123 |
throws InterruptedException { |
124 |
if (tasks == null) |
125 |
throw new NullPointerException(); |
126 |
List<Future<T>> futures = new ArrayList<Future<T>>(tasks.size()); |
127 |
boolean done = false; |
128 |
try { |
129 |
for (Callable<T> t : tasks) { |
130 |
FutureTask<T> f = new FutureTask<T>(t); |
131 |
futures.add(f); |
132 |
execute(f); |
133 |
} |
134 |
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 |
return futures; |
145 |
} finally { |
146 |
if (!done) |
147 |
for (Future<T> f : futures) |
148 |
f.cancel(true); |
149 |
} |
150 |
} |
151 |
|
152 |
public <T> List<Future<T>> invokeAll(Collection<Callable<T>> tasks, |
153 |
long timeout, TimeUnit unit) |
154 |
throws InterruptedException { |
155 |
if (tasks == null || unit == null) |
156 |
throw new NullPointerException(); |
157 |
long nanos = unit.toNanos(timeout); |
158 |
List<Future<T>> futures = new ArrayList<Future<T>>(tasks.size()); |
159 |
boolean done = false; |
160 |
try { |
161 |
for (Callable<T> t : tasks) { |
162 |
FutureTask<T> f = new FutureTask<T>(t); |
163 |
futures.add(f); |
164 |
execute(f); |
165 |
} |
166 |
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 |
long now = System.nanoTime(); |
179 |
nanos -= now - lastTime; |
180 |
lastTime = now; |
181 |
} |
182 |
} |
183 |
done = true; |
184 |
return futures; |
185 |
} finally { |
186 |
if (!done) |
187 |
for (Future<T> f : futures) |
188 |
f.cancel(true); |
189 |
} |
190 |
} |
191 |
|
192 |
} |