[英]schedule on task at a time - java
我有一个定期安排的任务。 有时可能会花费比预期更长的时间。 我试图找到一种方法来确保在任务已运行的情况下取消计划。 我检查的所有机制都会使任务等待并在第一个完成后运行它
锁定课程可以完成这项工作,但我正在寻找更高级的课程
任何想法
您可以使用ScheduledExecutorService
。 scheduleAtFixedRate
可能是你想要的东西,因为它会等待你的任务完成,当且仅当一个需要超过您指定的速度长:
如果此任务的任何执行花费的时间超过其周期,则后续执行可能会开始得较晚,但不会同时执行。
例:
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate(() -> {
// Body will be executed every second unless the previous task hasn't finished.
}, 0L, 1L, TimeUnit.SECONDS);
有一个叫做scheduleAtFixedRate和scheduleAtFixedDelay的东西。
scheduleAtFixedRate将在定义的时间启动另一个进程,因此,如果先前的进程未完成,则两个进程将在运行,并且可能导致两次运行同一事物的竞争状态。
一旦完成任务,scheduleAtFixedDelay将在固定时间后开始。
scheduleAtFixedRate与scheduleWithFixedDelay
在Spring中,您可以使用注释:
@Scheduled(fixedDelay =30000)
http://howtodoinjava.com/spring/spring-core/4-ways-to-schedule-tasks-in-spring-3-scheduled-example/
你知道Apache Camel框架吗? 它有一个名为quartz2的模块,可以安排任何任务。
尝试阅读此页面: http : //camel.apache.org/quartz2.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.