Scheduling a Job in Java using Timer class

Hi All,

In few applications some task need to run periodically, for example some data updater, cron jobs, notification tasks etc.
Java provide facility to schedule tasks as per requirement. This is example given below to schedule a simple task to display a message after 5 seconds
import java.util.*;

class MyTask extends TimerTask {

int count = 1;

public void run() {
//Add your task here, for a sample just printing the count in sysout below.
System.out.println(count+” : Task is runningh”);
count++;
}
}

class MyTaskScheduling {

public static void main(String[] args) {
Timer timerJob = new Timer();
timerJob.schedule(new Task(), 5000);
}
}

Thanks
R Vashi

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s