Wednesday 6 February 2019

How to create a new timer whose associated thread has the specified name and may be run as a daemon


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

ReminderTimerTask.java

import java.util.TimerTask;

public class ReminderTimerTask extends TimerTask
{
    public void run()
    {
        System.out.println("Wake up...");
    }
}

TimerDemo.java

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

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

        /*
         *
         * Creates a new timer whose associated thread has the
         * specified name, and may be specified to run as a daemon.
         *
         * Parameters:
         *
         * name - the name of the associated thread
         *
         * isDaemon - true if the associated thread should run as a
         * daemon
         *
         */

        Timer timer = new Timer("Reminder Timer", true);

        TimerTask reminderTimerTask = new ReminderTimerTask();

        timer.schedule(reminderTimerTask, new Date());
        System.out.println("Timer has schedule the reminderTimerTask...");

    }

}

Output:

Wake up...
Timer has schedule the reminderTimerTask...

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

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java_spring_2019/src/1ebed8b1986b12a20c2bf9302a0adc45079f1aad/Java_2019/TimerDemo_name_isDaemon/?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