Monday 4 February 2019

How to Schedule a task for repeated execution, beginning at the specified time


Click here to watch on Youtube: 
https://www.youtube.com/watch?v=sh8bSlDpBgU&list=UUhwKlOVR041tngjerWxVccw

CleanUpTimerTask.java

import java.util.TimerTask;

public class CleanUpTimerTask extends TimerTask
{
    public void run()
    {
        System.out.println("Clean up files...");
    }
}

TimerDemo.java

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

public class TimerDemo
{
    public static void main(String[] args)
    {
        TimerTask cleanUpTimerTask = new CleanUpTimerTask();
        Timer timer = new Timer();

        Date firstTime = new Date(System.currentTimeMillis() + 5000);
        System.out.println("firstTime = " + firstTime);

        /*
         * Schedules the specified task for repeated fixed-rate
         * execution, beginning at the specified time.
         *
         * Parameters:
         *
         * task - task to be scheduled.
         *
         * firstTime - First time at which task is to be executed.
         *
         * period - time in milliseconds between successive task
         * executions.
         */

        timer.scheduleAtFixedRate(cleanUpTimerTask, firstTime, 2000);
        System.out.println("Timer has schedule the cleanUpTimerTask...");
    }

}

Output:

firstTime = Sun Jan 20 10:36:17 IST 2019
Timer has schedule the cleanUpTimerTask...
Clean up files...
Clean up files...
Clean up files...
Clean up files...
Clean up files...
Clean up files...
Clean up files...
Clean up files...

Click the below link to download the code:
https://sites.google.com/site/javaspringram2019/java_spring_2019/TimerDemo_Schedule_firsttime_repeat_scheduleAtFixedRate.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java_Spring_2019/tree/master/Java_2019/TimerDemo_Schedule_firsttime_repeat_scheduleAtFixedRate

Bitbucket Link:
https://bitbucket.org/ramram43210/java_spring_2019/src/2db5cd1f4e7d5ab55ad7af886d6164700143c614/Java_2019/TimerDemo_Schedule_firsttime_repeat_scheduleAtFixedRate/?at=master

See also:
  • All JavaEE Videos Playlist
  • All JavaEE Videos
  • All JAVA EE Links
  • Spring Tutorial
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • Cooking Tutorial
  • 4 comments:

    1. IEEE Final Year Project centers make amazing deep learning final year projects ideas for final year students Final Year Projects for CSE to training and develop their deep learning experience and talents.

      IEEE Final Year projects Project Centers in India are consistently sought after. Final Year Students Projects take a shot at them to improve their aptitudes, while specialists like the enjoyment in interfering with innovation.

      corporate training in chennai corporate training in chennai

      corporate training companies in india corporate training companies in india

      corporate training companies in chennai corporate training companies in chennai

      I have read your blog its very attractive and impressive. I like it your blog. Digital Marketing Company in Chennai Project Centers in Chennai

      ReplyDelete
    2. This article are supper help full if you want to know more about First Time Manager Program then please click here.

      ReplyDelete
    3. This article are supper help full if you want to know more about First Time Manager Program then please click here.

      ReplyDelete