commit | 633a3aa26fbe9dc40df880ba4ffa7b863f11e473 | [log] [tgz] |
---|---|---|
author | magjed <magjed@webrtc.org> | Mon Nov 16 05:12:30 2015 -0800 |
committer | Commit bot <commit-bot@chromium.org> | Mon Nov 16 13:12:36 2015 +0000 |
tree | bf40456ce221e7113f33432c0d938bce4b600246 | |
parent | e155ae671cc69ad398685e78807edcd9e89087b1 [diff] |
ThreadUtils: Add joinUninterruptibly() with timeout This is similar to com.google.common.util.concurrent.Uninterruptibles.joinUninterruptibly(). http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/util/concurrent/Uninterruptibles.html#joinUninterruptibly(java.lang.Thread,%20long,%20java.util.concurrent.TimeUnit) Review URL: https://codereview.webrtc.org/1444273002 Cr-Commit-Position: refs/heads/master@{#10651}
diff --git a/talk/app/webrtc/java/android/org/webrtc/ThreadUtils.java b/talk/app/webrtc/java/android/org/webrtc/ThreadUtils.java index b078cb8..bd4fa44 100644 --- a/talk/app/webrtc/java/android/org/webrtc/ThreadUtils.java +++ b/talk/app/webrtc/java/android/org/webrtc/ThreadUtils.java
@@ -88,6 +88,29 @@ } } + public static boolean joinUninterruptibly(final Thread thread, long timeoutMs) { + final long startTimeMs = SystemClock.elapsedRealtime(); + long timeRemainingMs = timeoutMs; + boolean wasInterrupted = false; + while (timeRemainingMs > 0) { + try { + thread.join(timeRemainingMs); + break; + } catch (InterruptedException e) { + // Someone is asking us to return early at our convenience. We can't cancel this operation, + // but we should preserve the information and pass it along. + wasInterrupted = true; + final long elapsedTimeMs = SystemClock.elapsedRealtime() - startTimeMs; + timeRemainingMs = timeoutMs - elapsedTimeMs; + } + } + // Pass interruption information along. + if (wasInterrupted) { + Thread.currentThread().interrupt(); + } + return !thread.isAlive(); + } + public static void joinUninterruptibly(final Thread thread) { executeUninterruptibly(new BlockingOperation() { @Override