gpt4 book ai didi

java - 一次安排任务 - java

转载 作者:行者123 更新时间:2023-11-30 06:51:40 25 4
gpt4 key购买 nike

我有一个定期安排的任务。有时可能需要比预期更长的时间。我正在尝试找到一种方法来确保在任务已经运行的情况下取消调度。我检查的所有机制都会让任务等待并在第一次完成后运行它

锁定当然可以,但我正在寻找更高级别的东西

任何想法

最佳答案

您可以使用ScheduledExecutorServicescheduleAtFixedRate可能是您想要的,因为它会等待您的任务完成,如果一个任务花费的时间比您指定的速率要长:

If any execution of this task takes longer than its period, then subsequent executions may start late, but will not concurrently execute.

例子:

ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();

scheduler.scheduleAtFixedRate(() -> {
// Body will be executed every second unless the previous task hasn't finished.
}, 0L, 1L, TimeUnit.SECONDS);

关于java - 一次安排任务 - java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39936671/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com