57 |
|
if (ntasks == 0) |
58 |
|
throw new IllegalArgumentException(); |
59 |
|
List<Future<T>> futures= new ArrayList<Future<T>>(ntasks); |
60 |
< |
ExecutorCompletionService<T> ecs = |
60 |
> |
ExecutorCompletionService<T> ecs = |
61 |
|
new ExecutorCompletionService<T>(this); |
62 |
|
|
63 |
|
// For efficiency, especially in executors with limited |
79 |
|
int active = 1; |
80 |
|
|
81 |
|
for (;;) { |
82 |
< |
Future<T> f = ecs.poll(); |
82 |
> |
Future<T> f = ecs.poll(); |
83 |
|
if (f == null) { |
84 |
|
if (ntasks > 0) { |
85 |
|
--ntasks; |
86 |
|
futures.add(ecs.submit(it.next())); |
87 |
|
++active; |
88 |
|
} |
89 |
< |
else if (active == 0) |
89 |
> |
else if (active == 0) |
90 |
|
break; |
91 |
|
else if (timed) { |
92 |
|
f = ecs.poll(nanos, TimeUnit.NANOSECONDS); |
96 |
|
nanos -= now - lastTime; |
97 |
|
lastTime = now; |
98 |
|
} |
99 |
< |
else |
99 |
> |
else |
100 |
|
f = ecs.take(); |
101 |
|
} |
102 |
|
if (f != null) { |
111 |
|
ee = new ExecutionException(rex); |
112 |
|
} |
113 |
|
} |
114 |
< |
} |
114 |
> |
} |
115 |
|
|
116 |
|
if (ee == null) |
117 |
|
ee = new ExecutionException(); |
118 |
|
throw ee; |
119 |
|
|
120 |
|
} finally { |
121 |
< |
for (Future<T> f : futures) |
121 |
> |
for (Future<T> f : futures) |
122 |
|
f.cancel(true); |
123 |
|
} |
124 |
|
} |
133 |
|
} |
134 |
|
} |
135 |
|
|
136 |
< |
public <T> T invokeAny(Collection<Callable<T>> tasks, |
137 |
< |
long timeout, TimeUnit unit) |
136 |
> |
public <T> T invokeAny(Collection<Callable<T>> tasks, |
137 |
> |
long timeout, TimeUnit unit) |
138 |
|
throws InterruptedException, ExecutionException, TimeoutException { |
139 |
|
return doInvokeAny(tasks, true, unit.toNanos(timeout)); |
140 |
|
} |
153 |
|
} |
154 |
|
for (Future<T> f : futures) { |
155 |
|
if (!f.isDone()) { |
156 |
< |
try { |
157 |
< |
f.get(); |
156 |
> |
try { |
157 |
> |
f.get(); |
158 |
|
} catch (CancellationException ignore) { |
159 |
|
} catch (ExecutionException ignore) { |
160 |
|
} |
164 |
|
return futures; |
165 |
|
} finally { |
166 |
|
if (!done) |
167 |
< |
for (Future<T> f : futures) |
167 |
> |
for (Future<T> f : futures) |
168 |
|
f.cancel(true); |
169 |
|
} |
170 |
|
} |
171 |
|
|
172 |
< |
public <T> List<Future<T>> invokeAll(Collection<Callable<T>> tasks, |
173 |
< |
long timeout, TimeUnit unit) |
172 |
> |
public <T> List<Future<T>> invokeAll(Collection<Callable<T>> tasks, |
173 |
> |
long timeout, TimeUnit unit) |
174 |
|
throws InterruptedException { |
175 |
|
if (tasks == null || unit == null) |
176 |
|
throw new NullPointerException(); |
178 |
|
List<Future<T>> futures = new ArrayList<Future<T>>(tasks.size()); |
179 |
|
boolean done = false; |
180 |
|
try { |
181 |
< |
for (Callable<T> t : tasks) |
181 |
> |
for (Callable<T> t : tasks) |
182 |
|
futures.add(new FutureTask<T>(t)); |
183 |
|
|
184 |
|
long lastTime = System.nanoTime(); |
192 |
|
nanos -= now - lastTime; |
193 |
|
lastTime = now; |
194 |
|
if (nanos <= 0) |
195 |
< |
return futures; |
195 |
> |
return futures; |
196 |
|
} |
197 |
|
|
198 |
|
for (Future<T> f : futures) { |
199 |
|
if (!f.isDone()) { |
200 |
< |
if (nanos <= 0) |
201 |
< |
return futures; |
202 |
< |
try { |
203 |
< |
f.get(nanos, TimeUnit.NANOSECONDS); |
200 |
> |
if (nanos <= 0) |
201 |
> |
return futures; |
202 |
> |
try { |
203 |
> |
f.get(nanos, TimeUnit.NANOSECONDS); |
204 |
|
} catch (CancellationException ignore) { |
205 |
|
} catch (ExecutionException ignore) { |
206 |
|
} catch (TimeoutException toe) { |
215 |
|
return futures; |
216 |
|
} finally { |
217 |
|
if (!done) |
218 |
< |
for (Future<T> f : futures) |
218 |
> |
for (Future<T> f : futures) |
219 |
|
f.cancel(true); |
220 |
|
} |
221 |
|
} |