1

假设我有一个方法如下:

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();
    }
}

an 是如何InterruptedException被抛出(并陷入poll())的?可调用对象(包括InterruptedException,对吗?)抛出的任何东西都会是ExecutionException,我们永远不会取消任何期货,并且shutdownNow()永远不会调用服务。

顺便说一句:既然如此,是否有可能使这种轮询方法对诸如此类的事情更加防弹InterruptedException

4

2 回答 2

0

将在等待(阻塞)可调用完成时InterruptedException抛出。get

我不确定你所说的防弹是什么意思,你必须处理抛出异常的可能性。

于 2014-08-16T00:32:49.503 回答
0

InterruptedException 可以由调用 get 并等待完成的线程抛出,而不是由可调用线程抛出

于 2014-08-16T02:51:32.317 回答