Skip to content

Add Duration-based ctor to TimeoutKillableObserver #97

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

Merged
merged 1 commit into from
Aug 5, 2019
Merged
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 @@ -14,6 +14,9 @@

package com.google.copybara.shell;

import static com.google.common.base.Preconditions.checkNotNull;

import java.time.Duration;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand All @@ -31,7 +34,7 @@ public final class TimeoutKillableObserver implements KillableObserver {
private static final Logger log =
Logger.getLogger(TimeoutKillableObserver.class.getCanonicalName());

private final long timeoutMS;
private final Duration timeout;
private Killable killable;
private SleeperThread sleeperThread;
private boolean timedOut;
Expand All @@ -40,7 +43,11 @@ public final class TimeoutKillableObserver implements KillableObserver {
// provide a way to interrupt a thread

public TimeoutKillableObserver(final long timeoutMS) {
this.timeoutMS = timeoutMS;
this(Duration.ofMillis(timeoutMS));
}

public TimeoutKillableObserver(Duration timeout) {
this.timeout = checkNotNull(timeout, "timeout");
}

/**
Expand Down Expand Up @@ -74,9 +81,9 @@ private final class SleeperThread extends Thread {
@Override public void run() {
try {
if (log.isLoggable(Level.FINE)) {
log.fine("Waiting for " + timeoutMS + "ms to kill process");
log.fine("Waiting for " + timeout.toMillis() + "ms to kill process");
}
Thread.sleep(timeoutMS);
Thread.sleep(timeout.toMillis());
// timeout expired; kill it
synchronized (TimeoutKillableObserver.this) {
if (killable != null) {
Expand Down