java left logo
java middle logo
java right logo
 

Home arrow Java SE Tips
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 3942
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
Schedule task periodically E-mail
User Rating: / 84
PoorBest 

In some application some tasks need to be run periodically for example GUI screen should update the data from server periodically.

This java tips illustrate a method of scheduling a task periodically. Developers may use it for repetitive invoking of a method as per their requirements.

For this aim, first you have to make a class extending TimerTask abstract class and write code in run method, you want to run repetitively.

import java.util.TimerTask;

public class HeartBeatTask extends TimerTask{

  private int timerInterval;

  public HeartBeatTask(int timeInterval){
    this.timerInterval=timeInterval;
  }

  public void run() {
    // add the task here
  }

}

In your main program you can call this code to schedule task:

  java.util.Timer t1 = new java.util.Timer();

  HeartBeatTask tt = new HeartBeatTask(timeInterval);
  t1.schedule(tt, 0, timeInterval);

Now the task will repeat after the fixed time interval.


 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.