gpt4 book ai didi

java - 你如何有效地每 x 分钟重复一个 Action ?

转载 作者:搜寻专家 更新时间:2023-10-31 08:05:44 25 4
gpt4 key购买 nike

我有一个在 JBoss 中运行的应用程序。我有一个将更新 ArrayList 的传入 Web 服务请求。我想每 60 秒从另一个类(class)轮询一次这个列表。这样做最有效的方法是什么?

谁能给我举个好例子?

最佳答案

我也会推荐 ScheduledExecutorService ,它提供了比 TimerTimerTask 更高的灵 active ,包括使用多线程配置服务的能力。这意味着如果某个特定任务需要很长时间才能运行,它不会阻止其他任务的开始。

// Create a service with 3 threads.
ScheduledExecutorService execService = Executors.newScheduledThreadPool(3);

// Schedule a task to run every 5 seconds with no initial delay.
execService.scheduleAtFixedRate(new Runnable() {
public void run() {
System.err.println("Hello, World");
}
}, 0L, 5L, TimeUnit.SECONDS);

关于java - 你如何有效地每 x 分钟重复一个 Action ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1784331/

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