From b0848314531333d9673db32d9b22b3d413f15175 Mon Sep 17 00:00:00 2001 From: Janak Ramakrishnan Date: Mon, 22 Aug 2016 14:09:18 +0000 Subject: Use guava Uninterruptibles#joinUninterruptibly utility method instead of rolling our own in Consumers. -- MOS_MIGRATED_REVID=130936036 --- .../google/devtools/build/lib/shell/Consumers.java | 60 +++++++++------------- 1 file changed, 23 insertions(+), 37 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/com/google/devtools/build/lib/shell/Consumers.java b/src/main/java/com/google/devtools/build/lib/shell/Consumers.java index 972bfa8004..47515d7d1f 100644 --- a/src/main/java/com/google/devtools/build/lib/shell/Consumers.java +++ b/src/main/java/com/google/devtools/build/lib/shell/Consumers.java @@ -13,6 +13,7 @@ // limitations under the License. package com.google.devtools.build.lib.shell; +import com.google.common.util.concurrent.Uninterruptibles; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.IOException; @@ -221,45 +222,30 @@ class Consumers { @Override public void waitForCompletion() throws IOException { - boolean wasInterrupted = false; try { - while (true) { - try { - future.get(); - break; - } catch (InterruptedException ie) { - wasInterrupted = true; - // continue waiting - } catch (ExecutionException ee) { - // Runnable threw a RuntimeException - Throwable nested = ee.getCause(); - if (nested instanceof RuntimeException) { - final RuntimeException re = (RuntimeException) nested; - // The stream sink classes, unfortunately, tunnel IOExceptions - // out of run() in a RuntimeException. If that's the case, - // unpack and re-throw the IOException. Otherwise, re-throw - // this unexpected RuntimeException - final Throwable cause = re.getCause(); - if (cause instanceof IOException) { - throw (IOException) cause; - } else { - throw re; - } - } else if (nested instanceof OutOfMemoryError) { - // OutOfMemoryError does not support exception chaining. - throw (OutOfMemoryError) nested; - } else if (nested instanceof Error) { - throw new Error("unhandled Error in worker thread", ee); - } else { - throw new RuntimeException("unknown execution problem", ee); - } + Uninterruptibles.getUninterruptibly(future); + } catch (ExecutionException ee) { + // Runnable threw a RuntimeException + Throwable nested = ee.getCause(); + if (nested instanceof RuntimeException) { + final RuntimeException re = (RuntimeException) nested; + // The stream sink classes, unfortunately, tunnel IOExceptions + // out of run() in a RuntimeException. If that's the case, + // unpack and re-throw the IOException. Otherwise, re-throw + // this unexpected RuntimeException + final Throwable cause = re.getCause(); + if (cause instanceof IOException) { + throw (IOException) cause; + } else { + throw re; } - } - } finally { - // Read this for detailed explanation: - // http://www.ibm.com/developerworks/java/library/j-jtp05236/ - if (wasInterrupted) { - Thread.currentThread().interrupt(); // preserve interrupted status + } else if (nested instanceof OutOfMemoryError) { + // OutOfMemoryError does not support exception chaining. + throw (OutOfMemoryError) nested; + } else if (nested instanceof Error) { + throw new Error("unhandled Error in worker thread", ee); + } else { + throw new RuntimeException("unknown execution problem", ee); } } } -- cgit v1.2.3