Reputation: 63
I've been using Schedule Executor service to run my code every 3 seconds. Is there a way to run it every 3 seconds for 24 hours in my case? My code looks something like this. It isn't important what it does so I've just removed it all. The only part I care about is one List<String>
and the reason for that is after this 24 hour period I would then like to do some more stuff with this list.
public static void main(String[] args) throws Exception {
ScheduledExecutorService ses = Executors.newScheduledThreadPool(2);
ses.scheduleWithFixedDelay(new Runnable() {
// nonImportantVariables here
List<String> importantList = new ArrayList<String>();
public void run() {
//Not important code
}, 0, 3, TimeUnit.SECONDS);
// AFTER 24 HOURS I WOULD LIKE TO PERFORM TASKS ON IMPORTANT LIST
}
}
I've thought about adding a loop so it runs 28,800 times (that is how many 3 seconds are in 24 hours) but this seems a silly fix around and also I'd run into the issue that this wouldn't account for how long the code takes to run so would end up being more.
A second idea I had was the first time the iteration is run I can use a Date object. Using this Date object I could somehow add 24 hours onto this and have some method that compares the current Date object to the Date object at first iteration + 24 hours.
I can't seem to find anything in the documentation of Schedule executor service where I can set how many times to run a command. Any improvements to either of these ideas or a different solution is much appreciated thanks.
Upvotes: 0
Views: 172
Reputation: 7790
You can use another scheduler and in it you can submit another task that would run just once with a delay of 24 hours and it will do 2 things.
Upvotes: 1