I'm using the following code to schedule a timer (java.util.Timer):
Timer mytimer = new Timer("My Timer");
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, 12);
mytimer.schedule(mytask, c.getTime(), 24*60*60*1000);
I want the timer task to run everyday at 12:00 PM. My question is what will happen if the application is running AFTER 12:00. Let's say 16:00. Will the timer task run the next day at 12:00?
The documentation of the Timer Class states the following about the method public void schedule(TimerTask task, Date firstTime, long period)
In fixed-delay execution, each execution is scheduled relative to the actual execution time of the previous execution. If an execution is delayed for any reason (such as garbage collection or other background activity), subsequent executions will be delayed as well. In the long run, the frequency of execution will generally be slightly lower than the reciprocal of the specified period (assuming the system clock underlying Object.wait(long) is accurate). As a consequence of the above, if the scheduled first time is in the past, it is scheduled for immediate execution.
So we can understand from the above that the task will immediately be scheduled and executed and after that according to your program will be executed 24 hours later again. So if it is 16:00 then it will the executed immediately and will again be executed at 16:00 the next day.