Timers in java: Create timer in java with example?

Timer in java:-

Timer is java classe is in java.utils package, Timer is used to specify the task execution for every intervals or after holdup of some time in an application.

TimerTask in java:-

TimerTask is java class in java.utils package. TimerTask implements runnable interface which has run method to overried.
run method of TimerTask can have code for which you want to apply the timer functionality.This timer task can be scheduler by many times for specific time interval duration.

what is timers in java?

We have seen many scheduler programs in linux which are based on cron jobs. Java has also mechanism to provide the limited scheduling mechanism with Timer and TimerTask Task. if you want full pledge scheduler, you can use quartz scheduler.

Before this classes are introduced, we have to write our own custom code for scheduler task using our own calculation logic using threads.

Timer can be used in real time applications where you have large object which want to synch with database for every one hour or read the logs from the apache logs and insert into db for every 4 hours.

Timer class implements Runnable method which is for creating thread. so Timer will create threads internally which cause to execute the given task at periodic intervals of time have the control for execution of multiple task achieves the task execution at repeated intervals or we can post pone the execution for specific intervals of time.

How to create a Non Dameon timer?

Non Dameon timer is default timer implementaion using default constructor of Timer.
Timer t=new Timer();
Timer timer=Timer(false)
suppose you have created timer that means threads are created internally, once the run method of the thread execution is completed, timer should not executed
and other case is, you have to execute the timer for 10 seconds only, after that you have to cancel the timer, meaning is developer have the control to stop the execution of the timer(using cancel method of timer).
This timer causes performance effect as JVM unable to stop if non dameon timer is executing

How to create a Dameon timer?

Dameon timer are created in the background thread where it's timer is created and started.Dameon timer can be created using the following code
Timer timer=Timer(true);

Dameon timer is timer which will not be stopped even your applicaiton life ends.

As timer is completed dependten on thteads, so we will see the main difference.

difference between Dameon Thread and Non Dameon Thread?

Dameon Thread are background thread which are create and executed in background. scope is JVM running time only
Non Dameon Thread scope is application running life time. Once the application is stopped, dameon thread stopped it's execution.
This threads ends when exit method is called or run method execution is over or exception is occurred.

Timer example in java:-

how to execute timer for repeated interval of time:-

Let us see the example for usage of timer class with printing hello world message for every one second for 10 times.

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

public class HelloMessage extends TimerTask {
 private int numberoftimes = 0;
 public void run() {
  if (numberoftimes <= 10) {
   System.out.println("Hello World....");
  } else {
class TimerExample {
 public static void main(String[] args) {
  Timer helloWorldTimer = new Timer();
  helloWorldTimer.scheduleAtFixedRate(new HelloMessage(), 0, 1000);
Timer objec is create called the timer scheduleAtFixedRate() which has TimerTask job which has actual code written to execute for timer. For this we have written Cusotm java class HelloMessage which extends TimerTask and overrieds run method exectution. we have used cancel method to end the life of the timer after repeated 10 times execution.

Hope you got enough idea on basic timers in java for your execution.
Please free to comment if you have any questions or if you liked my post.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.