Wednesday 30 January 2019

Timer and TimerTask Intro V2 | How to schedule a task once or repeated using Timer and TimerTask


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

Click the below Image to Enlarge:
Timer and TimerTask Intro V2 | How to schedule a task once or repeated using Timer and TimerTask

CleanUpTask.java

import java.util.TimerTask;

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

EmailTask.java

import java.util.TimerTask;

public class EmailTask extends TimerTask
{
    public void run()
    {
        System.out.println("Send emails..");
    }
}

TimerDemo.java

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

public class TimerDemo
{
    public static void main(String[] args) throws InterruptedException
    {

        Timer timer = new Timer();
        TimerTask cleanUpTask = new CleanUpTask();
        TimerTask emailTask = new EmailTask();

        /*
         * One time execution
         */

        timer.schedule(emailTask, 3000);
 
        /*
         * Repeated execution
         */

        timer.schedule(cleanUpTask, 5000, 2000);

        System.out.println("Timer has schedule the tasks...");

    }

}

Output

Timer has schedule the tasks...
Send emails..
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...
Clean up files...

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

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

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