How do you effectively repeat an action every x minutes?

I have an application that works in JBoss. I have an incoming web service request that will update ArrayList. I want to poll this list from another class every 60 seconds. What would be the most effective way to do this?

Can someone give me a good example?

+3
source share
6 answers

I would also recommend a ScheduledExecutorService , which provides increased flexibility regarding Timerand TimerTaskincluding the ability to configure the service using multiple threads. This means that if a specific task takes a long time to complete, it will not prevent other tasks from starting.

// 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);
+13
source

Like abyx , Timerand TimerTaskis a good easy solution to run a class at a specific interval. If you need a heavy duty scheduler, can I suggest Quartz . This is an enterprise level job planner. It can easily handle thousands of scheduled tasks. As I said, this may be redundant for your situation.

+9

Timer TimerTask. .

+5

. java.util.Timer. , .

0
0

. , EJB. , JBoss. , .

However, if the EJB specification does not work, saving a state like ArrayList is also incompatible, so if you are just reading any static variable anyway, especially using the container timer service is likely to be redundant.

0
source

Source: https://habr.com/ru/post/1723677/


All Articles