The java.util.Timer
class does not have the functionality to do this using repeated tasks:
- You cannot configure a
Timer
to >>stop<< running repeated tasks at a given time - The execution model for repeated tasks is “fixed-delay execution”; i.e. a new task is scheduled based on the end-point of the previous task. That results in time slippage … if tasks take a long time.
The simple solution is:
- create a single
Timer
object - use
schedule(TimerTask task, Date time)
in a loop to schedule 48 separate tasks, starting on each hour in the range that you require.
According to the javadoc, Timer
is scalable and should be able to cope with lots of scheduled tasks efficiently.
The above does not deal with the requirement that each task runs for 30 minutes. It is not clear what that actually means, but if you want the task to run for no more than 30 minutes, then you need to implement a “watch dog” that interrupts the thread running the task when its time is up. The task needs to be implemented to check for and/or handle thread interrupts appropriately.
solved Java Timer going off every hour Sat-Sun