java - java - 在Java中,如何在执行后自动重新启动可运行列表
问题描述
我有一个List
可运行的,由服务返回。这些可运行文件以固定速率安排,延迟一分钟。代码如下所示:
public class Migrator {
// Define one minute in milliseconds.
private static final long INITIAL_DELAY = 60000;
private static final long UPDATE_RATE = 60000;
public static void main(String[] args) {
final ScheduledExecutorService executorService = Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors());
Set<String> dbNames = getDbNames();
dbNames.forEach(db -> {
List<Runnable> runnableList = new MigrationTaskManager(db).getRunnableTasksForDB();
runnableList.forEach(runnable -> {
executorService.scheduleAtFixedRate(runnable, INITIAL_DELAY, UPDATE_RATE, TimeUnit.MILLISECONDS);
});
});
}
}
执行后,它按预期工作,并且每分钟执行一次可运行文件,如下所示:
有什么方法可以让runnables
它们在完成执行后自动重新启动(无论它们花费多少执行时间),而不是必须使用executorService.scheduleAtFixedRate(runnable, INITIAL_DELAY, UPDATE_RATE, TimeUnit.MILLISECONDS)
?我真的可以在这方面使用一些帮助。
解决方案
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,
long initialDelay,
long delay,
TimeUnit unit);
这种可以输入延迟的方法,在执行完成+延迟后重新启动runnable。因此,只需延迟 0 即可解决您的问题:
executorService.scheduleWithFixedDelay(runnable, 1, 1, TimeUnit.NANOSECONDS);
推荐阅读
- xml - 如果没有互联网,如何删除 android: layout_marginTop = ""?
- react-native - 当服务器卡在 while 循环中时,客户端没有收到发出的值
- python - 为什么接受用户值列表的“快速排序”函数的“a”参数抛出错误:“NameError”?
- python - 注册后将用户重定向到登录页面
- python - AWS 安全组
- python - 尝试使用 matplotlib 可视化 SOM 时显示错误“RGBA 的长度应为 3 或 4”
- redis - 我们如何将 Spark 结构化流连接到 Redis?
- python - tensorflow_dataset ValueError:int() 的无效文字,基数为 10:'will'
- python - 如何让 selenium 使用 python 同时按下 shift + enter 的键盘?
- javascript - 为什么我在尝试将作为文本接收的正文分配给 javascript 中的变量时出现 Uncaught TypeError: response.text is not a function" 错误