Java 从期货抛出的中断异常
假设我有一个如下的方法:Java 从期货抛出的中断异常,java,polling,scheduledexecutorservice,interrupted-exception,Java,Polling,Scheduledexecutorservice,Interrupted Exception,假设我有一个如下的方法: public void poll(Callable<Boolean> callable) { ScheduledExecutorService service = Executors.newSingleThreadedScheduledExecutor(); Future<Boolean> future = service.schedule(callable, 0L, TimeUnit.MILLISECONDS); tr
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如何在轮询中抛出和捕获?callable抛出的任何东西,包括InterruptedException,对吗?将是一个ExecutionException,我们从不取消任何期货,也从不调用服务的Shutdownow
旁白:既然如此,有没有可能让这个轮询方法对InterruptedException这样的事情更加防弹?在等待调用完成时,get会抛出InterruptedException
我不知道你说的防弹是什么意思,你必须处理引发异常的可能性。InterruptedException可以由调用get并等待完成的线程引发,而不是由可调用的线程引发