Scheduling a Timer Task to Run Repeatedly

int delay = 5000; // delay for 5 sec. int period = 1000; // repeat every sec. Timer timer = new Timer(); timer.scheduleAtFixedRate(new TimerTask() { public void run() { // Task here ... } }, delay, period);

Comments

6 Feb 2010 - 2:25am by Anonymous (not verified)

import ....?
Explanations nil.

11 Feb 2010 - 1:07am by Anonymous (not verified)

Use Eclipse, then you dont have to mess with imports.
The package that contains Timer and TimerTask is in java.util

9 Mar 2010 - 5:18am by Anonymous (not verified)

Nice

6 Apr 2010 - 8:07am by Student (not verified)

Simple and enough

perfect....

19 Apr 2010 - 2:38am by Student (not verified)

is it running?

21 Apr 2010 - 10:45pm by pranav waila (not verified)

yup.. running

21 Apr 2010 - 10:47pm by pranav waila (not verified)

yup running well...

26 Apr 2010 - 12:29am by Hachilles (not verified)

This is for programmer who wants to understatnd the concept with code.
gud enough for simple sceduler

8 May 2010 - 10:35pm by Atul (not verified)

bro its not working i tried

try these codes

/*

Class : HeartBeatTask.java
Author : Atul
Date : May 09 Sunday

*/

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
System.out.println("Repeat : In Run");
}

}

/*

Class : Test Timer.java
Author : Atul
Date : May 09 Sunday

*/

import java.util.TimerTask;

class TestTimer
{
public static void main(String[] args)
{
System.out.println("In Test Timer Class!");

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

/*

Set the Timer Interval for the scheduler
in terms for milliseconds

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

*/
HeartBeatTask tt = new HeartBeatTask(100);
t1.schedule(tt, 0, 100);
}
}

3 Sep 2010 - 12:09am by wangjiming (not verified)

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.