[英]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.