简体   繁体   English

重新启动ScheduledThreadPoolExecutor中已取消的任务

[英]Restarting cancelled tasks in ScheduledThreadPoolExecutor

I am creating tasks with ScheduledThreadPoolExecutor and adding the Futures to a list as below in my ThreadFactory class. 我正在使用ScheduledThreadPoolExecutor创建任务,并将Future添加到ThreadFactory类中的列表中,如下所示。

private static List<Future> futures;
........
ScheduledFuture sf = executor.scheduleAtFixedRate(obj, delayInMilliSec, repeatPeriod, TimeUnit.MILLISECONDS);

futures.add(sf);

Now when I want to cancel all the tasks , I do as below 现在,当我想取消所有任务时,请按以下步骤操作

public void cancelAllTasks(){

  Iterator<Future> fi = futures.iterator();

  while(fi.hasNext()){

     fi.next().cancel(true);
  }

}

Now how do I restart these tasks at a later point of time ? 现在如何在以后的时间重新启动这些任务?

AFAik您不能,您需要重新安排他们的时间

Once a future is cancelled, the task cannot be resurrected at a later stage. 一旦取消将来,该任务将无法在以后的阶段复活。 A quick look at javadoc will explain the contract of the Future. 快速浏览一下javadoc将解释 Future的契约。

To restart the tasks, schedule them again with the executor. 要重新启动任务,请使用执行程序再次安排它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM