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.

Ads

Tutorials   
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? Learn Java Programming for beginners 5 Reasons Java is Still a Great Language to Boost Your Career Prospect as a Programmer How to learn Java with no programming experience? How to learn Java? What are Classes and Interfaces in Java? Converting a String to Integer in Java How to check if a string exists in a string in JavaScript? Installing JDK 8 on Windows 10 and configuring the settings Video Tutorial of reading file in Java 9 Invincible Reasons to learn Java New to Java programming Java Programming Tips, Articles and Notes How to download JDK 1.6? Encapsulation in Java Programming How to format number in Java? Generics Example Program in Java Java 9 Features Java 9 Tutorial, news and examples How to read content of JPEG file in Java? Image Processing Tutorials in Java Programming Language Introduction to Java Technology Java 8 Consumer Interface with forEach Loop Introduction to Generics in Java Java 8 consumer class(interface) example Oracle Launches Project Valhalla for Java How to read properties file in Java? How to add JDK 8 support in Eclipse? Java 8: Java 8 is officially released and it can be downloaded How to Invoke method using Reflection API of Java? First Lambda Expressions in Java

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

Advertisements

Ads
Share on Google+Share on Google+

Task Scheduling in JAVA

Posted on: July 29, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Task Scheduling in JAVA   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:4
TQ
October 21, 2011
bug

I got the above code to work with the slight change of timer.schedule( new Task(), 0, 1000); where 0 is the initial delay.
Vijay
January 28, 2013
Incorrect output

The above program gives incorrect output which is not same as show above.
3configurators
June 25, 2012
error

this code doesn't work
vishal Anekar
September 11, 2013
learn java language

guidance about implementation of k-map using graphics in java

Ads

 

Ads