Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从期货抛出的中断异常_Java_Polling_Scheduledexecutorservice_Interrupted Exception - Fatal编程技术网

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并等待完成的线程引发,而不是由可调用的线程引发