您如何有效地每隔X分钟重复一次操作? [英] How do you efficiently repeat an action every x minutes?
本文介绍了您如何有效地每隔X分钟重复一次操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个在JBoss中运行的应用程序.我有一个传入的Web服务请求,该请求将更新ArrayList
.我想每60秒从另一个班级轮询一次此列表.最有效的方法是什么?
I have an application that runs in JBoss. I have an incoming web service request that will update an ArrayList
. I want to poll this list from another class every 60 seconds. What would be the most efficient way of doing this?
有人能给我指出一个好榜样吗?
Could anyone point me to a good example?
推荐答案
I would also recommend ScheduledExecutorService, which offers increased flexibility over Timer
and TimerTask
including the ability to configure the service with multiple threads. This means that if a specific task takes a long time to run it will not prevent other tasks from commencing.
// 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);
这篇关于您如何有效地每隔X分钟重复一次操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文