Wednesday, March 16, 2011

Implementing Timer and TimerTask in Java

Implementing Timer and TimerTask in Java

Dear Reader,
Here is a simple working code which you can utilize in your application for running a task in Timer/Scheduler interval:

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class TestTimer {
static final Timer t = new Timer();
public static void main(String[] args) {
System.out.println("Testing Timer by Deepak.Modi");

TimerTask tt = new TimerTask() {
public void run() {
//This run block will get executed in every interval. So You can put your method here for repeatitive calls.
System.out.println("--- Timer is called --");
t.scheduleAtFixedRate(tt, new Date(), 5000);  //The fixed interval when your run method executes is 5000 milliseconds. 
synchronized (TestTimer.class) {
try {
catch (InterruptedException ie) {

No comments:

Post a Comment