129 |
|
// Record exceptions so that if we fail to obtain any |
130 |
|
// result, we can throw the last exception we got. |
131 |
|
ExecutionException ee = null; |
132 |
< |
long lastTime = timed ? System.nanoTime() : 0; |
132 |
> |
final long deadline = timed ? System.nanoTime() + nanos : 0L; |
133 |
|
Iterator<? extends Callable<T>> it = tasks.iterator(); |
134 |
|
|
135 |
|
// Start one task for sure; the rest incrementally |
151 |
|
f = ecs.poll(nanos, TimeUnit.NANOSECONDS); |
152 |
|
if (f == null) |
153 |
|
throw new TimeoutException(); |
154 |
< |
long now = System.nanoTime(); |
155 |
< |
nanos -= now - lastTime; |
156 |
< |
lastTime = now; |
154 |
> |
nanos = deadline - System.nanoTime(); |
155 |
|
} |
156 |
|
else |
157 |
|
f = ecs.take(); |
236 |
|
for (Callable<T> t : tasks) |
237 |
|
futures.add(newTaskFor(t)); |
238 |
|
|
239 |
< |
long lastTime = System.nanoTime(); |
239 |
> |
final long deadline = System.nanoTime() + nanos; |
240 |
|
|
241 |
|
// Interleave time checks and calls to execute in case |
242 |
|
// executor doesn't have any/much parallelism. |
243 |
|
Iterator<Future<T>> it = futures.iterator(); |
244 |
|
while (it.hasNext()) { |
245 |
|
execute((Runnable)(it.next())); |
246 |
< |
long now = System.nanoTime(); |
247 |
< |
nanos -= now - lastTime; |
250 |
< |
lastTime = now; |
251 |
< |
if (nanos <= 0) |
246 |
> |
nanos = deadline - System.nanoTime(); |
247 |
> |
if (nanos <= 0L) |
248 |
|
return futures; |
249 |
|
} |
250 |
|
|
251 |
|
for (Future<T> f : futures) { |
252 |
|
if (!f.isDone()) { |
253 |
< |
if (nanos <= 0) |
253 |
> |
if (nanos <= 0L) |
254 |
|
return futures; |
255 |
|
try { |
256 |
|
f.get(nanos, TimeUnit.NANOSECONDS); |
259 |
|
} catch (TimeoutException toe) { |
260 |
|
return futures; |
261 |
|
} |
262 |
< |
long now = System.nanoTime(); |
267 |
< |
nanos -= now - lastTime; |
268 |
< |
lastTime = now; |
262 |
> |
nanos = deadline - System.nanoTime(); |
263 |
|
} |
264 |
|
} |
265 |
|
done = true; |