如何阻止下一个线程在ScheduledThreadPoolExecutor中运行 [英] How to stop next thread from running in a ScheduledThreadPoolExecutor
问题描述
我有一个ScheduledThreadPoolExecutor,它有一个线程并且每30秒运行一次。
I have a ScheduledThreadPoolExecutor which has one thread and runs for every 30 seconds.
现在,如果当前正在执行的线程抛出一些异常,那么我需要确保下一个线程没有运行且ScheduledThreadPoolExecutor关闭。
Now, if the current executing thread throws some exception, then I need to make sure that the next thread do not run and the the ScheduledThreadPoolExecutor is down.
我如何实现这个目标?
推荐答案
关闭()
启动有序关闭,其中执行先前提交的任务,但不接受任何新任务。如果已经关闭,调用没有其他影响。
此方法不等待先前提交的任务完成执行。 使用awaitTermination执行此操作。
shutdownNow()
尝试停止所有正在执行的任务,停止等待任务的处理,并返回等待执行的任务列表。
此方法不等待主动执行任务终止。 使用awaitTermination执行此操作。
除尽力尝试停止处理主动执行任务外,无法保证。例如,典型的实现将通过Thread.interrupt()取消,因此任何未能响应中断的任务可能永远不会终止。
参考这些帖子有关工作代码的详细信息。
Refer to these post for more details with working code.
这篇关于如何阻止下一个线程在ScheduledThreadPoolExecutor中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!