你如何每x分钟有效地重复一次动作?

5 java jboss

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

有谁能指出我一个很好的例子?

Ada*_*ski 16

我还建议使用ScheduledExecutorService,它提供了更高的灵活性Timer,TimerTask包括使用多个线程配置服务的能力.这意味着如果特定任务需要很长时间才能运行,则不会阻止其他任务开始.

// 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);
Run Code Online (Sandbox Code Playgroud)


Poi*_*ter 9

正如abyx所发布的,Timer并且TimerTask是一个很好的轻量级解决方案,以一定的间隔运行一个类.如果你需要一个重型调度程序,我可以建议Quartz.它是一个企业级作业调度程序.它可以轻松处理数千个预定的工作.就像我说的那样,这可能对你的情况有点过分了.


aby*_*byx 5

你可以使用TimerTimerTask.这里显示一个例子.