【发布时间】:2014-10-09 17:49:22
【问题描述】:
假设我有一个方法如下:
public void poll(Callable<Boolean> callable) {
ScheduledExecutorService service = Executors.newSingleThreadedScheduledExecutor();
Future<Boolean> future = service.schedule(callable, 0L, TimeUnit.MILLISECONDS);
try {
while (!future.get()) {
future = service.schedule(callable, 5L, TimeUnit.MINUTES);
}
} catch (ExecutionException e) {
// ...
} catch (InterruptedException e) {
// ...
} finally {
service.shutdown();
}
}
InterruptedException 是如何被抛出(并被poll() 捕获)的?可调用对象抛出的任何东西(包括InterruptedException,对吗?)都会是ExecutionException,我们永远不会取消任何期货,并且服务的shutdownNow() 永远不会被调用。
除此之外:既然如此,是否有可能使这种轮询方法对InterruptedException 之类的东西更加防弹?
【问题讨论】:
标签: java polling scheduledexecutorservice interrupted-exception