[英]Stopping and removing a task from ScheduledExecutorService
I know that I can throw an exception to suppress further executions of task that has been scheduled for repeated execution inside a ScheduledExecutorService (see this question ).我知道我可以抛出异常来抑制计划在 ScheduledExecutorService 内重复执行的任务的进一步执行(请参阅此问题)。
I also know that I can setRemoveOnCancelPolicy(true) to make sure cancelled tasks are removed from the queue.我也知道我可以 setRemoveOnCancelPolicy(true) 来确保从队列中删除取消的任务。
My questions are: Is the task actually removed from the scheduler also when I throw an exception from within it?我的问题是:当我从调度程序中抛出异常时,该任务是否实际上也从调度程序中删除了? Does this happen by implicitly cancelling the future?
这是通过隐式取消未来而发生的吗? If yes, does this mean that setRemoveOnCancelPolicy() also bears on this case?
如果是,这是否意味着 setRemoveOnCancelPolicy() 也适用于这种情况?
Couldn't find anything in the Javadocs.在 Javadocs 中找不到任何内容。
I was wondering the same, couldn't find anything in the docs, so I tried it out.我也想知道,在文档中找不到任何内容,所以我试了一下。 Observation:
观察:
RuntimeException
marks the future as done , not cancelled .RuntimeException
将未来标记为完成,而不是取消。Runnable
is removed from the scheduler's queue, regardless of setRemoveOnCancelPolicy()
.setRemoveOnCancelPolicy()
什么, Runnable
都会从调度程序的队列中setRemoveOnCancelPolicy()
。 Try it out yourself:自己试试:
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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.