What is Timer and TimerTask in Java

Introduction

Timer in Java is a utility class which is used to schedule tasks for both one time and repeated execution. Timer is similar to alarm facility many people use in mobile phone. Just like you can have one time alarm or repeated alarm, You can use java.util.Timer to schedule one time task or repeated task. In fact we can implement a Reminder utility using Timer in Java and that's what we are going to see in this example of Timer in Java. Two classes java.util.Timer and java.util.TimerTask is used to schedule jobs in Java and forms Timer API. TimerTask is actual task which is executed by Timer. Similar to Thread in Java, TimerTask also implements Runnable interface and overrides run method to specify task details. This Java tutorial will also highlight difference between Timer and TimerTask class and explains how Timer works in Java. By the way difference between Timer and Thread is also a popular Java questions on fresher level interviews.

What is Timer and TimerTask in Java ?

Timer in Java is a utility class form java.util package which provides facility to schedule task at any time in future. As I said earlier, Timer is analogues to alarm clock you setup in your smartphone. Just like alarm can be either one time or recurring, You can also schedule task for one time and recurring time interval using Timer API. Timer provides method to schedule Task where task is instance of TimerTask class, which implements Runnable interface and overrides run() method to define task which is called on scheduled time.

How Timer works in Java ?

Timer class in Java maintains a background Thread (this could be either daemon thread or user thread, based on how you created your Timer object), also called as timer's task execution thread. For each Timer there would be corresponding task processing Thread which run scheduled task at specified time. If your Timer thread is not daemon then it will stop your application from exits until it completes all schedule task. Its recommended that TimerTask should not be very long otherwise it can keep this thread busy and not allow other scheduled task to get completed. This can delay execution of other scheduled task, which may queue up and execute in quick succession once offending task completed.

Difference between Timer and TimerTask in Java

I have seen programmers getting  confused between Timer and TimerTask, which is quite unnecessary because these two are altogether different. You just need to remember:

1) Timer in Java schedules and execute TimerTask which is an implementation of Runnable interface and overrides run method to defined actual task performed by that TimerTask.

2) Both Timer and TimerTask provides cancel() method. Timer's cancel() method cancels whole timer while TimerTask's one cancels only a particular task. I think this is the wroth noting difference between Timer and TimerTask in Java.

Canceling Timer in Java

You can cancel Java Timer by calling cancel() method of java.util.Timer class, this would result in following:
1) Timer will not cancel any currently executing task.
2) Timer will discard other scheduled task and will not execute them.
3) Once currently executing task will be finished, timer thread will terminate gracefully.
4) Calling Timer.cancel() more than one time will not affect. second call will be ignored.

Comments

Popular posts from this blog

How to Install AnyDesk remote desktop client on Ubuntu

How to install Jaspersoft Studio on Eclipse

What is Advanced Encryption Standard (AEC) and online tool to encrypt and decrypt data using AEC.