Mar*_*coS 11
我在过去使用外部进程生成类似的东西Runtime.getRuntime().exec(command).我想你可以在你的方法中做这样的事情:
Timer timer = new Timer(true);
InterruptTimerTask interruptTimerTask =
new InterruptTimerTask(Thread.currentThread());
timer.schedule(interruptTimerTask, waitTimeout);
try {
// put here the portion of code that may take more than "waitTimeout"
} catch (InterruptedException e) {
log.error("timeout exeeded);
} finally {
timer.cancel();
}
Run Code Online (Sandbox Code Playgroud)
这是 InterruptTimerTask
/*
* A TimerTask that interrupts the specified thread when run.
*/
protected class InterruptTimerTask extends TimerTask {
private Thread theTread;
public InterruptTimerTask(Thread theTread) {
this.theTread = theTread;
}
@Override
public void run() {
theTread.interrupt();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15438 次 |
| 最近记录: |