Scheduling a timer task to run repeatedly
We moved. Please visit this link for this post.
I am going to show example of how to use TimerTask class:
package com.faynasoftlabs.tutorial.java.timertaskexample;
import java.util.Timer;
import java.util.TimerTask;
/**
* Class which shows how to use timertask class
* @author FaYna Soft Labs
*/
public class Main {
public static void main(String[] args) throws Exception {
int delay = 5000; // delay for 5 sec.
int period = 1000; // repeat every sec.
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
System.out.println("done");
}
}, delay, period);
}
}
This code will show done every second. I put 5 sec delay for first showing. It is very simple example of using TimerTask.
Categories: Java, Java 1.6 Tutorial
Comments (0)
Trackbacks (0)
Leave a comment
Trackback