Monday, December 5, 2016

Schedule task using Java

We can schedule a task using Java such that it will run everyday or on specified day at specified time.

Language Used:
Java

Git Repo:
https://github.com/csanuragjain/extra/tree/master/ScheduleTask

Program:

Main method:
      public static void main(String[] args) {  
           // TODO Auto-generated method stub  
           System.out.println("Program started at "+new Date());  
           Calendar calendar = Calendar.getInstance();  
     calendar.add(Calendar.MINUTE,1);  
     TimerTask task = new TimerTask() {  
       @Override  
       public void run() {  
            System.out.println("Task started at "+new Date());  
            System.out.println("Hello !!!");  
       }  
      };  
      runTask(calendar,task);  
      }  

How it works:
1) We print the current data and time
2) We make a calender object which will point to current time+1 minute
3) We make a new task using TimerTask class
4) We define task content in the run method.
5) So if the task run it will print the time at which task ran along with a message Hello
6) We call the method runTask which actually schedule the task defined in step4 at time defined at step2

runTask method:
      public static void runTask(Calendar calendar, TimerTask task)  
      {  
           Timer time = new Timer();   
     time.schedule(task, calendar.getTime(), TimeUnit.SECONDS.toMillis(15));  
      }  

How it works:
1) We define an object of Timer
2) We call the schedule method. It takes 3 argument
3) First argument takes the task to be run
4) Second argument defines the time after which task will start running. We made it as current time +1
5) Third argument tells the time after which task run again after it completes. Here we made it as 15 seconds.
6) So finally when you run the program, task starts after 1 minutes and after completion it will rerun again after every 15 seconds

Output:
 Program started at Mon Dec 05 18:40:55 IST 2016  
 Task started at Mon Dec 05 18:41:55 IST 2016  
 Hello !!!  
 Task started at Mon Dec 05 18:42:10 IST 2016  
 Hello !!!  
 Task started at Mon Dec 05 18:42:25 IST 2016  
 Hello !!!  
 Task started at Mon Dec 05 18:42:40 IST 2016  
 Hello !!!  

Full Program:
 package com.cooltrickshome;  
 import java.util.Calendar;  
 import java.util.Date;  
 import java.util.Timer;  
 import java.util.TimerTask;  
 import java.util.concurrent.TimeUnit;  
 public class ScheduleTask {  
      /**  
       * @param args  
       */  
      public static void main(String[] args) {  
           // TODO Auto-generated method stub  
           System.out.println("Program started at "+new Date());  
           Calendar calendar = Calendar.getInstance();  
     calendar.add(Calendar.MINUTE,1);  
     TimerTask task = new TimerTask() {  
       @Override  
       public void run() {  
            System.out.println("Task started at "+new Date());  
            System.out.println("Hello !!!");  
       }  
      };  
      runTask(calendar,task);  
      }  
      public static void runTask(Calendar calendar, TimerTask task)  
      {  
           Timer time = new Timer();   
     time.schedule(task, calendar.getTime(), TimeUnit.SECONDS.toMillis(15));  
      }  
 }  

Hope it helps :)

1 comment:

  1. On-line competitors in each Area of interest is rising exponentially. Standing out from the teeming on-line plenty by using your distinctive voice is sweet for enterprise. Luckily, Audio is dust low cost in case you are on a funds and simply beginning out on-line. If you want to learn more about this topic, then please visit https://onlineconvertfree.com/convert-format/3g2-to-mp4/

    ReplyDelete