I have created a task that I need to query database for List of String and store it in neededDataList
. I will use neededDataList
in a scheduled task every 10 minutes to do a simple task.
And I need to retrieve a fresh data of neededDataList
every 1 hour. Will the scheduledExecutorService2
pick up the fresh neededDataList
?
Can someone review my code if it will cause some problem?
Assuming my code has its main method and it will call the run(Database database)
:
public class Handler {
private static final ScheduledExecutorService scheduledExecutorService1 = MDScheduledExecutorService.wrap(
new ScheduledThreadPoolExecutor(1), "ses-1-thread-%d");
private static final ScheduledExecutorService scheduledExecutorService2 = MDScheduledExecutorService.wrap(
new ScheduledThreadPoolExecutor(5), "ses-2-thread-%d");
private List<String> neededDataList;
private void run(Database database) {
scheduledExecutorService1.scheduleAtFixedRate(() -> {
neededDataList= database.getNeededData();
}, 1, 60, TimeUnit.MINUTES);
scheduledExecutorService2.scheduleAtFixedRate(new HandlerImpl(), 1, 10, TimeUnit.MINUTES);
}
private class HandlerImpl implements Runnable {
@Override
public void run() {
executeTask(neededDataList);
}
}
}