Monday, April 17, 2017

How to run a task in java periodically ?



How to schedule a task in java ?

Few java applications are required to update something after a period of time - and that too automatically or without manual intervention.

To do so, in Java, we can perform below steps to complete this task,


------------------------------ScheduleTask.java---------------------------



public class ScheduledTask extends TimerTask {
public void run() {

System.out.println(" scheduler enabled on " + new Date().getTime());

//you can perform any operation required as per your need here

}
}



--------------------RunScheduledTask.java------------------------------

public class RunScheduledTask{

    public static void main(String args[]){

                Timer time = new Timer(); // Instantiate Timer Object
ScheduleTask st = new ScheduleTask();
time.schedule(st, 0, 86400000);// scheduled for one day

    }
}



No comments:

Post a Comment

Fixing yum command on linux

You may find yourself having to fix more packages. So you can just remove everything you had installed via  pip and reinstall everythin...