Monday 4 February 2019

How to Schedule a task for repeated execution, beginning after specified delay


Click here to watch on Youtube: 
https://www.youtube.com/watch?v=AwNaXL9XrcA&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.Timer;
import java.util.TimerTask;

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

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

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

}

Output:

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...
Clean up files...
Clean up files...

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

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java_spring_2019/src/2db5cd1f4e7d5ab55ad7af886d6164700143c614/Java_2019/TimerDemo_ScheduleAtFixedRate_delay_repeat/?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
  • No comments:

    Post a Comment