Timers in java: Create timer in java with example?

The timer in java:-

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

TimerTask in java:-

TimerTask is java class in java.utils package. TimerTask implements a runnable interface which has run method to override.
the 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 are timers in java?

We have seen many scheduler programs in Linux which are based on cron jobs. Java has also a 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.

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

Timer class implements a Runnable method which is for creating a 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 tasks achieves the task execution at repeated intervals or we can postpone the execution for specific intervals of time.

How to create a Non-Dameon timer?

Non-Dameon timer is default timer implementation 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 execute
and another 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 the non-Dameon timer is executing

How to create a Dameon timer?

Dameon timer is 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 a timer which will not be stopped even your application life ends.

As the timer is completed dependent on threads, so we will see the main difference.

difference between Dameon Thread and Non-Dameon Thread?

Dameon Thread is a background thread which is created and executed in the background. the scope is JVM running time only

Non-Dameon Thread scope is an application running lifetime. Once the application is stopped, Dameon thread stopped its execution.

These threads ends when the exit method is called or run method execution is over or exception occurs.

Timer example in java:-

**how to execute timer for the 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 object is created called the timer scheduleAtFixedRate() which has TimerTask job which has actual code written to execute for the timer. For this, we have written Custom java class HelloMessage which extends TimerTask and overrides run method execution. 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.

Similar Posts