I'm having a few issues with scheduling a task to run in my code every minute.
I have tried many ways to do this such as:
Timer timer = new Timer();
timer.schedule( new TimerTask() {
public void run() {
printTime();
}
}, 0, 60*1000);
And also:
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
Runnable task = () -> {
printTime();
};
executor.scheduleWithFixedDelay(task, 0, 1, TimeUnit.MINUTES);
These both print out erratic times not even close to the times I have provided:
2019-12-03T12:48:23.300
2019-12-03T12:48:24.700
2019-12-03T12:48:41.895
2019-12-03T12:48:42.799
2019-12-03T12:48:47.189
2019-12-03T12:48:47.211
2019-12-03T12:48:47.278
These are the times I get, totally wrong and some are within seconds! Is there an accurate way for me to schedule a task to run every minute while my code preforms other tasks?
My plan was to calculate the times between when the task is fired but Timer and ScheduledExecutor seems to wait longer than the time provided also not only shorter. So this means it would also be off.