Ad

Tuesday 30 July 2013

Timer and TimerTask in Java - Choosing the Correct Schedule Method(Schedule and scheduleAtFixedRate)

Timer API provides the ability to
  1. Schedule   the Job/Task to run at the specified time
  2. Schedule the Job/Task to run repeatedly with specified interval of Time
Timer API Provides 6 methods to achieve this.Big Question arise to all is , which method we need
to choose.The selection of the method depends on our requirement matching the  below criteria
  • We need to schedule the task for the  single  execution or repeated execution
  • We need the task to be scheduled at the specified time or to be after the specified delay in milliseconds
  • We need a fixed delay execution or fixed rate execution

Method public void schedule(TimerTask task,long delay)
Description To execute the task after the specified delay in milliseconds.So choose this method,if your requirement is to schedule for the single time execution and to be run after the specified delay

Method public void schedule(TimerTask task, Date time)
Description To execute the task at specified time.So choose this method,if your requirement is to schedule for the single time execution and need to scheduled at the specified time

Method public void schedule(TimerTask task,long delay,long interval)
Description To execute the task repeatedly with specified interval of time.The First execution of the task will start after the specified delay in milliseconds.Each task will run relative to the previous execution time(ie previous task execution time+interval).So use this method,if you requirement is for the repeated execution and need to be a fixed delay execution and also the first execution has to be started after the specified delay

Method public void schedule(TimerTask task,Date firstTime,long interval)
Description To execute the task repeatedly with specified interval of time.The First execution of the task will start at the time(firstTime) specified.Each task will run relative to the previous execution time(ie previous task execution time+interval).So use this method,if you requirement is for the repeated execution and need to be a fixed delay execution and also the first execution has to be started at the specified time

Method scheduleAtFixedRate(TimerTask task, Date firstTime, long interval)
Description To execute the task repeatedly with specified interval of time.The First execution of the task will start at the time(firstTime) specified.Each task will run relative to the initial task execution time(Initial task executiontime + taskcompletedcount*interval).So use this method,if you requirement is for the repeated execution and need to be a fixed rate execution and also the first execution has to be started at the specified time

Method scheduleAtFixedRate(TimerTask task, long delay, long interval)
Description To execute the task repeatedly with specified interval of time.The First execution of the task will start after the specified delay in milliseconds.Each task will run relative to the initial task execution time(Initial task executiontime + taskcompletedcount*interval).So use this method,if you requirement is for the repeated execution and need to be a fixed rate execution and also the first execution has to be started after the specified delay

For the sample program on Timer Task,please refer this Post Timer Task-Example

Related Topics:
Example - Timer and TimerTask
Exception - Task already scheduled or cancelled