aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStephan Herhut <herhut@google.com>2017-10-19 14:47:58 +0000
committerGerrit Code Review <noreply-gerritcodereview@google.com>2017-10-19 14:47:58 +0000
commit5ce3868403341603adeeccb80b4edefbf7d2e1b0 (patch)
tree116681544088cf1eae3699aa2b08fa944d460788
parenta63b19277f33bbb4901ce84a248a59e77ed378ba (diff)
parent3cc1e698c02a458986ab573d43d09bc626655d51 (diff)
downloadr8-5ce3868403341603adeeccb80b4edefbf7d2e1b0.tar.gz
Merge "In ThreadUtils.awaitFutures, cancel remaining threads if one throws."
-rw-r--r--src/main/java/com/android/tools/r8/utils/ThreadUtils.java27
1 files changed, 17 insertions, 10 deletions
diff --git a/src/main/java/com/android/tools/r8/utils/ThreadUtils.java b/src/main/java/com/android/tools/r8/utils/ThreadUtils.java
index 8fe3af652..59abfdb45 100644
--- a/src/main/java/com/android/tools/r8/utils/ThreadUtils.java
+++ b/src/main/java/com/android/tools/r8/utils/ThreadUtils.java
@@ -3,9 +3,7 @@
// BSD-style license that can be found in the LICENSE file.
package com.android.tools.r8.utils;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.List;
+import java.util.Iterator;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@@ -13,17 +11,26 @@ import java.util.concurrent.Future;
public class ThreadUtils {
- public static <T> List<T> awaitFutures(Collection<? extends Future<? extends T>> futures)
+ public static void awaitFutures(Iterable<? extends Future<?>> futures)
throws ExecutionException {
- ArrayList<T> result = new ArrayList<>(futures.size());
- for (Future<? extends T> f : futures) {
+ Iterator<? extends Future<?>> it = futures.iterator();
+ try {
+ while (it.hasNext()) {
+ it.next().get();
+ }
+ } catch (InterruptedException e) {
+ throw new RuntimeException("Interrupted while waiting for future.", e);
+ } finally {
+ // In case we get interrupted or one of the threads throws an exception, abort all further
+ // work, if possible.
try {
- result.add(f.get());
- } catch (InterruptedException e) {
- throw new RuntimeException("Interrupted while waiting for future.", e);
+ while (it.hasNext()) {
+ it.next().cancel(true);
+ }
+ } catch (Throwable t) {
+ // Ignore the new Exception.
}
}
- return result;
}
public static ExecutorService getExecutorService(int threads) {