178 |
|
List<Future<T>> futures = new ArrayList<Future<T>>(tasks.size()); |
179 |
|
boolean done = false; |
180 |
|
try { |
181 |
< |
for (Callable<T> t : tasks) { |
182 |
< |
FutureTask<T> f = new FutureTask<T>(t); |
183 |
< |
futures.add(f); |
184 |
< |
execute(f); |
185 |
< |
} |
181 |
> |
for (Callable<T> t : tasks) |
182 |
> |
futures.add(new FutureTask<T>(t)); |
183 |
> |
|
184 |
|
long lastTime = System.nanoTime(); |
185 |
+ |
|
186 |
+ |
// Interleave time checks and calls to execute in case |
187 |
+ |
// executor doesn't have any/much parallelism. |
188 |
+ |
Iterator<Future<T>> it = futures.iterator(); |
189 |
+ |
while (it.hasNext()) { |
190 |
+ |
execute((Runnable)(it.next())); |
191 |
+ |
long now = System.nanoTime(); |
192 |
+ |
nanos -= now - lastTime; |
193 |
+ |
lastTime = now; |
194 |
+ |
if (nanos <= 0) |
195 |
+ |
return futures; |
196 |
+ |
} |
197 |
+ |
|
198 |
|
for (Future<T> f : futures) { |
199 |
|
if (!f.isDone()) { |
200 |
< |
if (nanos < 0) |
200 |
> |
if (nanos <= 0) |
201 |
|
return futures; |
202 |
|
try { |
203 |
|
f.get(nanos, TimeUnit.NANOSECONDS); |