Home > Java, Java 1.6 Tutorial > Scheduling a timer task to run repeatedly

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.

About these ads
Categories: Java, Java 1.6 Tutorial
  1. No comments yet.
  1. No trackbacks yet.

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

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: