Task Scheduling in JAVA

Ads
 
Tutorials   
What are the prerequisites for learning Java? How to replace all occurrences of a string in JavaScript? How to convert a stack trace to a string? Example code of reading file line by line in Java with Apache Commons IO library How to store unique values in Java using Set? java.nio ShortBuffer Example How to use Java Path class? Java NIO Package Tutorial How to limit object creation in Java? Java 11 String lines function example to split it in lines Top upcoming features of JDK 11 Java/JDK 11 Tutorial How to install JDK 10 in Ubuntu 18.04? Install JDK 10 on Windows Java 10 features Processing large file in Java Learning Java for non programmers Why You Should Learn Java in 2018? Installing JDK 9 on Windows 10 How to convert binary to decimal in Java? How to add two numbers in Java? What is Applet in Java with Example? What is nested class in Java with Example? Exception handling in Java Programming Examples What is Constructor Overloading in Java? How to display image in Swing? What is Java programming used for? How to create custom exception in Java? How to set CLASSPATH in Java? What is Multithreading in Java? IO Streams in Java with Examples How to convert String Date to Timestamp in Java? Best Java Tutorial for Beginners Read user input in Java using Scanner Java Programming Examples Read file line by line in Java 8 What are the benefits of learning Java Programming? A brief history and future of Java programming language Importance of Java for Mobile Application Development How To Become A Good Software Developer?

Task Scheduling in JAVA

In some applications some task need to run periodically, for example a application of report generating checks for new database entry after one day and make reports according to the entries then save all entries in company's permanent record.

In some applications some task need to run periodically, for example a application of report generating checks for new database entry after one day and make reports according to the entries then save all entries in company's permanent record.

Task Scheduling in JAVA

     

In some applications some task need to run periodically, for example a application of report generating checks for new database entry after one day and make reports according to the entries then save all entries in company's permanent record.

Java provide facility to schedule tasks as per requirement. This is example given below to schedule a simple task to display a message after 3 seconds.

Methods and classes used in this example :

java.util.TimerTask class specifies a task that can be scheduled to run once or after scheduled time. We can define action to be performed by this timer task. by using run() method of java.util.TimeTask. This is protected method.

java.util.Timer class provides facility to schedule tasks for future execution in a background thread. In this example we have used schedule() method of this class that specifies task for execution after the specified delay. It takes time delay as a parameter in milliseconds.

Note : Use ctrl + C to exit from this program.

TaskScheduling.java

import java.util.*;
class Task extends TimerTask {
  
    int count = 1;
      // run is a abstract method that defines task performed at scheduled time.
    public void run() {
        System.out.println(count+" : Mahendra Singh");
	    count++;
    }
}
class TaskScheduling {
   public static void main(String[] args) {
       Timer timer = new Timer();
   
       // Schedule to run after every 3 second(3000 millisecond)
       timer.schedule( new Task(), 3000);	
   }
}


Output of the program :

Download Source Code

Ads

Ads