Scheduler in Java | Code Factory


Donate : Link

Medium Blog : Link

Applications : Link

Run task periodically in java or Scheduler in Java.


File : SchedulerMain.java

package com.codeFactory;

import java.util.Date;
import java.util.TimerTask;
public class SchedulerMain extends TimerTask {
    Date current;
    int cnt = 0;
    @Override
    public void run() {
        current = new Date();
        System.out.println("Current Date and Time : " + current);
        cnt++;
        if (cnt == 5)
        {
            System.out.println("EXIT.");
            System.exit(0);
        }
    }
}

File : SchedulerCall.java

package com.codeFactory;

import java.util.Timer;
public class SchedulerCall {
    public static void main(String args[])
    {
        Timer time = new Timer();
        SchedulerMain scMain = new SchedulerMain();
        time.schedule(scMain, 0, 5000);
    }
}

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 )

Connecting to %s