如果超时发生但不是未来的计划任务,则结束当前运行的任务

时间:2016-03-08 09:17:38

标签: java scheduled-tasks

我有一些任务需要安排。我正在使用ScheduledExecutorService。我有一些要求。

  1. 某些任务按固定延迟安排。
  2. 某些任务在某个时间每天运行。
  3. 某些任务运行固定号码。天或固定没有。期间。
  4. 如果需要更多时间执行指定的时间
  5. ,则取消任务

    我已将前三个点实施为

       //schedule task at fixed delay or at certain time(by calculating the delay)
        final ScheduledFuture<?> a = executor.scheduleAtFixedRate(runnableClass,initialDelay, period, TimeUnit.SECONDS);
    
       //end task scheduler if it has executed for certain number of period ( endDelay` by calculting the timestamp to be cancelled)
    
        executor.schedule(new Runnable() {
                        public void run() {
                            a.cancel(false);
                        }
                    }, endDelay, TimeUnit.SECONDS);
    

    但是我对第4个要求感到困惑,该要求表示如果超过指定的执行时间则取消某个任务。我想取消正在运行的任务只有超时发生而不是未来的预定任务。请提出一些建议。

    我使用的是ScheduledExecutorService,不允许使用其他库。

0 个答案:

没有答案