简体   繁体   English

如何在特定时间停止/取消ScheduledExecutorService中的任务?

[英]How to stop/cancel a task in ScheduledExecutorService at particular time?

For example, I have a task which should be executed between 8:00-20:00 every minites every day. 例如,我有一个任务,每天每小部分在8:00-20:00之间执行。

so i calculate the time gap between 8:00 and the time which my app started for initialDelay , and use Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(command, initialDelay, 1, TimeUnit.MINUTE) . 所以我计算了8:00和我的应用为initialDelay启动的时间之间的时间间隔,并使用Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(command, initialDelay, 1, TimeUnit.MINUTE)

The question is, do I need the second timer to observe the task and cancel it when the clock comes to 20:00? 问题是,我是否需要第二个计时器来观察任务并在时钟为20:00时将其取消? or do I have to compare if the time is 20:00 at every time when the task's executed? 还是必须比较每次执行任务时的时间是否为20:00?

You probably need a real scheduler. 您可能需要一个真正的调度程序。

See Quartz https://quartz-scheduler.org 参见Quartz https://quartz-scheduler.org

It should fit your needs 它应该符合您的需求

For the cron syntax : http://quartz-scheduler.org/api/2.2.0/org/quartz/CronTrigger.html 对于cron语法: http : //quartz-scheduler.org/api/2.2.0/org/quartz/CronTrigger.html

Or with a fluent API http://quartz-scheduler.org/api/2.2.0/org/quartz/TriggerBuilder.html 或使用流畅的API http://quartz-scheduler.org/api/2.2.0/org/quartz/TriggerBuilder.html

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

相关问题 如何在特定时间段内使用 ScheduledExecutorService 运行任务? - How to run a task DURING PARTICULAR PERIOD OF TIME with ScheduledExecutorService? 如何使用 ScheduledExecutorService 每天在特定时间运行某些任务? - How to run certain task every day at a particular time using ScheduledExecutorService? 如何有效地取消定期的ScheduledExecutorService任务 - How to effectively cancel periodic ScheduledExecutorService task 如何使用 ScheduledExecutorService 取消当前任务? - How to cancel current task using ScheduledExecutorService? 是否有取消 ScheduledExecutorService 中的任务的选项? - is there an option to cancel a task in ScheduledExecutorService? 如何使用 ScheduledExecutorService 在一周内的每个星期五随时运行特定任务? - How to run a particular task every Friday in a week at any time using ScheduledExecutorService? 如何取消ScheduledExecutorService / Runnable以便在回压/退出活动时停止加载InterstitialAd - How to cancel ScheduledExecutorService/ Runnable to stop loading the InterstitialAd on backpress/exit the activity ScheduledExecutorService 在我的任务中取消任务 - ScheduledExecutorService cancel task inside my task 如何停止工作ScheduledExecutorService? - How to stop working ScheduledExecutorService? 如何停止ScheduledExecutorService? - How to stop a ScheduledExecutorService?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM