92 |
|
*/ |
93 |
|
private static class TaskGroupWaiter { |
94 |
|
private final ReentrantLock lock = new ReentrantLock(); |
95 |
< |
private final Condition monitor = lock.newCondition(); |
95 |
> |
private final Condition done = lock.newCondition(); |
96 |
|
private int firstIndex = -1; |
97 |
|
private int countDown; |
98 |
|
TaskGroupWaiter(int ntasks) { countDown = ntasks; } |
103 |
|
if (firstIndex < 0) |
104 |
|
firstIndex = index; |
105 |
|
if (--countDown == 0) |
106 |
< |
monitor.signalAll(); |
107 |
< |
} |
108 |
< |
finally { |
106 |
> |
done.signalAll(); |
107 |
> |
} finally { |
108 |
|
lock.unlock(); |
109 |
|
} |
110 |
|
} |
113 |
|
lock.lock(); |
114 |
|
try { |
115 |
|
while (countDown > 0) |
116 |
< |
monitor.await(); |
116 |
> |
done.await(); |
117 |
|
return firstIndex; |
118 |
< |
} |
120 |
< |
finally { |
118 |
> |
} finally { |
119 |
|
lock.unlock(); |
120 |
|
} |
121 |
|
} |
124 |
|
lock.lock(); |
125 |
|
try { |
126 |
|
while (countDown > 0 && nanos > 0) |
127 |
< |
nanos = monitor.awaitNanos(nanos); |
127 |
> |
nanos = done.awaitNanos(nanos); |
128 |
|
return firstIndex; |
129 |
< |
} |
132 |
< |
finally { |
129 |
> |
} finally { |
130 |
|
lock.unlock(); |
131 |
|
} |
132 |
|
} |
135 |
|
lock.lock(); |
136 |
|
try { |
137 |
|
return countDown <= 0; |
138 |
< |
} |
142 |
< |
finally { |
138 |
> |
} finally { |
139 |
|
lock.unlock(); |
140 |
|
} |
141 |
|
} |
158 |
|
} |
159 |
|
} |
160 |
|
|
161 |
< |
|
166 |
< |
/** |
167 |
< |
* Helper method to cancel unfinished tasks before return of |
168 |
< |
* bulk execute methods |
169 |
< |
*/ |
170 |
< |
private static void cancelUnfinishedTasks(List<Future<?>> futures) { |
171 |
< |
for (Future<?> f : futures) |
172 |
< |
f.cancel(true); |
173 |
< |
} |
174 |
< |
|
175 |
< |
/** |
176 |
< |
* Same as cancelUnfinishedTasks; Workaround for compiler oddity |
177 |
< |
*/ |
178 |
< |
private static <T> void cancelUnfinishedTasks2(List<Future<T>> futures) { |
179 |
< |
for (Future<T> f : futures) |
180 |
< |
f.cancel(true); |
181 |
< |
} |
182 |
< |
|
183 |
< |
// any/all methods, each a little bit different than each other |
161 |
> |
// any/all methods, each a little bit different than the other |
162 |
|
|
163 |
|
public List<Future<?>> runAny(Collection<Runnable> tasks) |
164 |
|
throws InterruptedException { |
183 |
|
Collections.swap(futures, first, 0); |
184 |
|
return futures; |
185 |
|
} finally { |
186 |
< |
cancelUnfinishedTasks(futures); |
186 |
> |
for (Future<?> f : futures) |
187 |
> |
f.cancel(true); |
188 |
|
} |
189 |
|
} |
190 |
|
|
213 |
|
Collections.swap(futures, first, 0); |
214 |
|
return futures; |
215 |
|
} finally { |
216 |
< |
cancelUnfinishedTasks(futures); |
216 |
> |
for (Future<?> f : futures) |
217 |
> |
f.cancel(true); |
218 |
|
} |
219 |
|
} |
220 |
|
|
241 |
|
return futures; |
242 |
|
} finally { |
243 |
|
if (!waiter.isDone()) |
244 |
< |
cancelUnfinishedTasks(futures); |
244 |
> |
for (Future<?> f : futures) |
245 |
> |
f.cancel(true); |
246 |
|
} |
247 |
|
} |
248 |
|
|
249 |
|
public List<Future<?>> runAll(Collection<Runnable> tasks, |
250 |
< |
long timeout, TimeUnit unit) |
250 |
> |
long timeout, TimeUnit unit) |
251 |
|
throws InterruptedException { |
252 |
|
if (tasks == null || unit == null) |
253 |
|
throw new NullPointerException(); |
269 |
|
return futures; |
270 |
|
} finally { |
271 |
|
if (!waiter.isDone()) |
272 |
< |
cancelUnfinishedTasks(futures); |
272 |
> |
for (Future<?> f : futures) |
273 |
> |
f.cancel(true); |
274 |
|
} |
275 |
|
} |
276 |
|
|
296 |
|
Collections.swap(futures, first, 0); |
297 |
|
return futures; |
298 |
|
} finally { |
299 |
< |
cancelUnfinishedTasks2(futures); |
299 |
> |
for (Future<T> f : futures) |
300 |
> |
f.cancel(true); |
301 |
|
} |
302 |
|
} |
303 |
|
|
325 |
|
Collections.swap(futures, first, 0); |
326 |
|
return futures; |
327 |
|
} finally { |
328 |
< |
cancelUnfinishedTasks2(futures); |
328 |
> |
for (Future<T> f : futures) |
329 |
> |
f.cancel(true); |
330 |
|
} |
331 |
|
} |
332 |
|
|
351 |
|
return futures; |
352 |
|
} finally { |
353 |
|
if (!waiter.isDone()) |
354 |
< |
cancelUnfinishedTasks2(futures); |
354 |
> |
for (Future<T> f : futures) |
355 |
> |
f.cancel(true); |
356 |
|
} |
357 |
|
} |
358 |
|
|
378 |
|
return futures; |
379 |
|
} finally { |
380 |
|
if (!waiter.isDone()) |
381 |
< |
cancelUnfinishedTasks2(futures); |
381 |
> |
for (Future<T> f : futures) |
382 |
> |
f.cancel(true); |
383 |
|
} |
384 |
|
} |
385 |
|
|