Когда будущее. /> код: список 7.10 Отмена задания с использованием будущего. < /p>
Код: Выделить всё
public static void timedRun(Runnable r,
long timeout, TimeUnit unit)
throws InterruptedException {
Future task = taskExec.submit(r);
try {
task.get(timeout, unit);
} catch (TimeoutException e) {
// task will be cancelled below
} catch (ExecutionException e) {
// exception thrown in task: rethrow
throw launderThrowable(e.getCause());
} finally {
// Harmless if task already completed
task.cancel(true); // interrupt if running
}
}
Подробнее здесь: https://stackoverflow.com/questions/796 ... t-a-thread