Java ScheduledExecutorService在超时后结束

Java ScheduledExecutorService在超时后结束,java,scheduled-tasks,java-threads,Java,Scheduled Tasks,Java Threads,你好!我有调度程序,我需要检查它的工作时间。我该怎么做?当调度程序工作超过5分钟时,我需要从someFunction()返回一个错误 ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); public int someFunction() { ScheduledFuture<Integer> future = scheduler.schedule(new Sche

你好!我有调度程序,我需要检查它的工作时间。我该怎么做?当调度程序工作超过5分钟时,我需要从someFunction()返回一个错误

ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);

public int someFunction() {
    ScheduledFuture<Integer> future = 
        scheduler.schedule(new ScheduledPrinter(), 10, TimeUnit.SECONDS);
    if (scheduler.isShutdown()) {
        return future.get();
    }
}
private class ScheduledPrinter implements Callable<Integer> {
    public Integer call() throws Exception {
        // do something
        if (!serverResponse.getErrorData().equals("QueueIsNotReady")) {
            scheduler.shutdown();
            return getResponse();
        } 
        return null;
    }
}
ScheduledExecutorService调度器=执行者。newScheduledThreadPool(1);
公共int函数(){
计划的未来未来=
scheduler.schedule(新ScheduledPrinter(),10,TimeUnit.SECONDS);
if(scheduler.isshutton()){
返回future.get();
}
}
私有类ScheduledInter实现可调用{
公共整数调用()引发异常{
//做点什么
如果(!serverResponse.getErrorData().equals(“QueueIsNotReady”)){
scheduler.shutdown();
返回getResponse();
} 
返回null;
}
}

您可以使用以下选项:

  • 调度程序。等待终止(5,时间单位。分钟)

  • 安排另一个任务在5分钟内运行,以检查第一个任务的状态。此方法可用于异步处理超时<代码>某些函数在这种情况下将立即返回:

    ScheduledFuture<Integer> future =
            scheduler.schedule(new ScheduledPrinter(), 10, TimeUnit.SECONDS);
    scheduler.schedule(() -> {
        if (!future.isDone()) {
            future.cancel(true);  // cancel task if it runs more than 5 minutes
        }
    }, 5, TimeUnit.MINUTES);
    
    计划未来=
    scheduler.schedule(新ScheduledPrinter(),10,TimeUnit.SECONDS);
    调度程序。调度(()->{
    如果(!future.isDone()){
    future.cancel(true);//如果任务运行超过5分钟,则取消该任务
    }
    },5,时间单位(分钟);
    

  • 您可以使用以下选项:

  • 调度程序。等待终止(5,时间单位。分钟)

  • 安排另一个任务在5分钟内运行,以检查第一个任务的状态。此方法可用于异步处理超时<代码>某些函数在这种情况下将立即返回:

    ScheduledFuture<Integer> future =
            scheduler.schedule(new ScheduledPrinter(), 10, TimeUnit.SECONDS);
    scheduler.schedule(() -> {
        if (!future.isDone()) {
            future.cancel(true);  // cancel task if it runs more than 5 minutes
        }
    }, 5, TimeUnit.MINUTES);
    
    计划未来=
    scheduler.schedule(新ScheduledPrinter(),10,TimeUnit.SECONDS);
    调度程序。调度(()->{
    如果(!future.isDone()){
    future.cancel(true);//如果任务运行超过5分钟,则取消该任务
    }
    },5,时间单位(分钟);
    

  • 你好nr.3没有问题吗。它实际上取消了整个
    ScheduledFuture
    ,而不是当前正在运行的任务?如何仅取消当前执行并保留计划?谢谢。First scheduler.schedule调用返回对第一个任务的引用,第二个调用调度第二个任务,该任务在5分钟内取消第一个任务(使用引用)。现在我注意到THOP实际上要关闭整个调度程序,我以前没有看到。我的坏消息和道歉。无论如何,您的第三个示例将不起作用,因为OP使用单个工作线程定义调度程序-如果运行
    ScheduledInter
    ,则检查未来是否完成的runnable将不会启动-没有可用的工作线程。Hi。nr.3没有问题吗。它实际上取消了整个
    ScheduledFuture
    ,而不是当前正在运行的任务?如何仅取消当前执行并保留计划?谢谢。First scheduler.schedule调用返回对第一个任务的引用,第二个调用调度第二个任务,该任务在5分钟内取消第一个任务(使用引用)。现在我注意到THOP实际上要关闭整个调度程序,我以前没有看到。我的坏消息和道歉。无论如何,您的第三个示例将不起作用,因为OP使用单个工作线程定义调度程序-如果运行
    ScheduledInter
    ,则检查未来是否完成的runnable将不会启动-没有可用的工作线程。
    ScheduledFuture<Integer> future =
            scheduler.schedule(new ScheduledPrinter(), 10, TimeUnit.SECONDS);
    scheduler.schedule(() -> {
        if (!future.isDone()) {
            future.cancel(true);  // cancel task if it runs more than 5 minutes
        }
    }, 5, TimeUnit.MINUTES);