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”);

class MyTaskScheduling {

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

R Vashi

Leave a Reply

Please log in using one of these methods to post your comment: Logo

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

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.