In some application some tasks need to be run periodically for example GUI screen should update the data from server periodically.
This java tips illustrate a method of scheduling a task periodically. Developers may use it for repetitive invoking of a method as per their requirements.
For this aim, first you have to make a class extending TimerTask abstract class and write code in run method, you want to run repetitively.
import java.util.TimerTask;
public class Shedule extends TimerTask{
public void run() {
// add the task here
}
}
In your main program you can call this code to schedule task:
java.util.Timer timer = new java.util.Timer();
Calendar date = Calendar.getInstance();
date.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
date.set(Calendar.HOUR, 0);
date.set(Calendar.MINUTE, 0);
date.set(Calendar.SECOND, 0);
date.set(Calendar.MILLISECOND, 0);
//sheduler run now
//timer.schedule(new Shedule(), 0, 60 * 60);
// Schedule to run every Sunday in midnight
timer.schedule(new Shedule(), date.getTime(), 1000 * 60 * 60 * 24 * 7);
Now the task will repeat after the fixed time interval.
more ref;
--> http://www.developer.com/java/other/article.php/3564211
Comments