我正在更新一些旧代码,但不确定复制下面的 Watchdog/TimeoutObserver 功能的最佳方式。但是,这是一种旧的方法,我正在尝试将其更新为更符合 jre7。任何建议或帮助将不胜感激。
import org.pache.tools.ant.util.Watchdog;
import org.pache.tools.ant.util.TimeoutObserver;
public class executer implemnts TimeoutObserver {
public String execute() throws Exception {
Watchdog watchDog = null;
try {
//instantiate a new watch dog to kill the process
//if exceeds beyond the time
watchDog = new Watchdog(getTimeout());
watchDog.addTimeoutObserver(this);
watchDog.start();
... Code to do the execution .....
} finally {
if (aWatchDog != null) {
aWatchDog.stop();
}
}
public void timeoutOccured(Watchdog arg0) {
killedByTimeout = true;
if (process != null){
process.destroy();
}
arg0.stop();
}
}