Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1315 Add awaitTerminationUninterruptibly(..) for ExecutorService - M… #3908

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static com.google.common.util.concurrent.Uninterruptibles.takeUninterruptibly;
import static com.google.common.util.concurrent.Uninterruptibles.tryAcquireUninterruptibly;
import static com.google.common.util.concurrent.Uninterruptibles.tryLockUninterruptibly;
import static com.google.common.util.concurrent.Uninterruptibles.awaitTerminationUninterruptibly;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;

Expand Down Expand Up @@ -465,6 +466,41 @@ public void testTryAcquireTimeoutMultiInterruptExpiredMultiPermit() {
assertInterrupted();
}

// executor.awaitTermination Testcases
public void testTryAwaitTerminationUninterruptibly() {
ScheduledExecutorService scheduledPool = Executors.newScheduledThreadPool(1);
requestInterruptIn(500);
scheduledPool.execute(new Runnable() {
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
scheduledPool.shutdown();
awaitTerminationUninterruptibly(scheduledPool, LONG_DELAY_MS, MILLISECONDS);
assertInterrupted();
}

public void testTryAwaitTerminationInfiniteTimeout() {
ScheduledExecutorService scheduledPool = Executors.newScheduledThreadPool(1);
requestInterruptIn(500);
scheduledPool.execute(new Runnable() {
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
scheduledPool.shutdown();
awaitTerminationUninterruptibly(scheduledPool);
assertInterrupted();
}

/**
* Wrapper around {@link Stopwatch} which also contains an "expected completion time." Creating a
* {@code Completion} starts the underlying stopwatch.
Expand Down
40 changes: 40 additions & 0 deletions guava/src/com/google/common/util/concurrent/Uninterruptibles.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.concurrent.CancellationException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -513,6 +514,45 @@ public static boolean tryLockUninterruptibly(Lock lock, long timeout, TimeUnit u
}
}

/** Invokes {@code queue.}{@link ExecutorService#awaitTermination(long, TimeUnit)} uninterruptibly. */
@GwtIncompatible // concurrency
@SuppressWarnings("GoodTime")
public static boolean awaitTerminationUninterruptibly(ExecutorService executor, long timeout, TimeUnit unit) {
boolean interrupted = false;
try {
long remainingNanos = unit.toNanos(timeout);
long end = System.nanoTime() + remainingNanos;
while (true) {
try {
return executor.awaitTermination(remainingNanos, unit);
} catch (InterruptedException e) {
interrupted = true;
remainingNanos = end - System.nanoTime();
}
}
} finally {
if (interrupted) {
Thread.currentThread().interrupt();
}
}
}

/**
* Invokes {@code queue.}{@link ExecutorService#awaitTermination(long, TimeUnit)} uninterruptibly.
* Infinite timeout version.
*/
@GwtIncompatible // concurrency
@SuppressWarnings("GoodTime") // should accept a java.time.Duration
public static boolean awaitTerminationUninterruptibly(ExecutorService executor) {
boolean interrupted = false;
while (true) {
interrupted = awaitTerminationUninterruptibly(executor, Long.MAX_VALUE, TimeUnit.NANOSECONDS);
if(interrupted) {
return interrupted;
}
}
}

// TODO(user): Add support for waitUninterruptibly.

private Uninterruptibles() {}
Expand Down