Explanation :
To run a file every one hour we need to follow these steps :
1) Create a class that will extend TimerTask
2) Implement run method. Your business logic will go into this method.
3) Set number of hours in the noOfHours field.
4) Make a call to schedule method of Timer class and pass 1 * 60 * 60 * 1000 (One hour in milliseconds).
To run a file every one hour we need to follow these steps :
1) Create a class that will extend TimerTask
2) Implement run method. Your business logic will go into this method.
3) Set number of hours in the noOfHours field.
4) Make a call to schedule method of Timer class and pass 1 * 60 * 60 * 1000 (One hour in milliseconds).
import java.util.Timer;
import java.util.TimerTask;
public class Scheduler extends TimerTask {
private int noOfHours;
public int getNoOfHours() {
return noOfHours;
}
public void setNoOfHours(int noOfHours) {
this.noOfHours = noOfHours;
}
public static void main(String args[]) {
Timer timer = new Timer();
Scheduler scheduler = new Scheduler();
// Set number of hours
scheduler.setNoOfHours(1);
timer.schedule(scheduler, 0, scheduler.getNoOfHours() * 60 * 1000 * 60);// No of hours in milliseconds
}
@Override
public void run() {
System.out.println("My task is running every " + this.getNoOfHours() * 60 + " minutes");
}
}
No comments:
Post a Comment