【问题标题】:Stopping and removing a task from ScheduledExecutorService从 ScheduledExecutorService 停止和删除任务
【发布时间】:2015-10-08 17:31:47
【问题描述】:

我知道我可以抛出异常来禁止进一步执行已安排在 ScheduledExecutorService 内重复执行的任务(请参阅this question)。

我也知道我可以 setRemoveOnCancelPolicy(true) 来确保从队列中删除取消的任务。

我的问题是:当我从调度程序中抛出异常时,该任务是否实际上也已从调度程序中删除?这是否通过隐式取消未来而发生?如果是,这是否意味着 setRemoveOnCancelPolicy() 也适用于这种情况?

在 Javadocs 中找不到任何内容。

【问题讨论】:

    标签: java executorservice scheduledexecutorservice


    【解决方案1】:

    我也想知道,在文档中找不到任何内容,所以我试了一下。观察:

    • 投掷RuntimeException 将未来标记为完成未取消
    • Runnable 已从调度程序的队列中删除,无论setRemoveOnCancelPolicy()

    自己试试吧:

    public class SchedulerTest {
        protected final Logger log = LoggerFactory.getLogger(this.getClass());
    
        @Test
        public void schedulerExecutionException() throws Exception {
            log.info("Test: schedulerExecutionException");
    
            ScheduledThreadPoolExecutor sched = new ScheduledThreadPoolExecutor(2);
            sched.setRemoveOnCancelPolicy(true);
    
            ScheduledFuture future1 = sched.scheduleAtFixedRate(new Runnable() {
                int counter = 0;
                @Override
                public void run() {
                    log.info("Runnable 1: "+ ++counter);
    
                    if (counter >= 2) {
                        log.info("Runnable 1: BOOOM");
                        throw new RuntimeException("boom");
                    }
    
                }
            }, 1, 1, TimeUnit.SECONDS);
    
            ScheduledFuture future2 = sched.scheduleAtFixedRate(new Runnable() {
                int counter = 0;
                @Override
                public void run() {
                    log.info("Runnable 2: "+ ++counter);
                }
            }, 1, 1, TimeUnit.SECONDS);
    
            long cutoff = new Date().getTime() + 6000;
    
            while (new Date().getTime() < cutoff) {
                log.info("Scheduler Queue size: "+ sched.getQueue().size());
                log.info("Future 1: is "+ (future1.isCancelled() ? "" : "not ") +"cancelled, is "+ (future1.isDone()? "" : "not ") +"done");
                log.info("Future 2: is "+ (future2.isCancelled() ? "" : "not ") +"cancelled, is "+ (future2.isDone()? "" : "not ") +"done");
                Thread.sleep(1000);
            }
            assertEquals(sched.getQueue().size(), 1);
    
            future2.cancel(true);
            log.info("Scheduler Queue size: "+ sched.getQueue().size());
            log.info("Future 1: is "+ (future1.isCancelled() ? "" : "not ") +"cancelled, is "+ (future1.isDone()? "" : "not ") +"done");
            log.info("Future 2: is "+ (future2.isCancelled() ? "" : "not ") +"cancelled, is "+ (future2.isDone()? "" : "not ") +"done");
    
            assertEquals(sched.getQueue().size(), 0);
    
            sched.shutdownNow();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-05
      • 1970-01-01
      • 2022-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多