Ques. How to set a timer in java?

- I have a job which I want to run in a schedule time like 30 minutes. I am thinking to keep a timer for that. How can I do that please suggest.

Posted on Jul 23, 2014 by Ruby Singh
Ans. java.util.Timer is a utility class that can be used to schedule a thread to be executed at certain time in future. Java Timer class can be used to schedule a task to be run one-time or to be run at regular intervals.

java.util.TimerTask is an abstract class that implements Runnable interface and we need to extend this class to create our own TimerTask that can be scheduled using java Timer class.

Timer class is thread safe and multiple threads can share a single Timer object without need for external synchronization. Timer class uses java.util.TaskQueue to add tasks at given regular interval and at any time there can be only one thread running the TimerTask, for example if you are creating a Timer to run every 10 seconds but single thread execution takes 20 seconds, then Timer object will keep adding tasks to the queue and as soon as one thread is finished, it will notify the queue and another thread will start executing.

Timer class uses Object wait and notify methods to schedule the tasks.

Here is an example of Timer and TimerTask implementation.

package com.withoutbook.threads;

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

public class MyTimerTask extends TimerTask {

@Override
public void run() {
System.out.println(\"Timer task started at:\"+new Date());
completeTask();
System.out.println(\"Timer task finished at:\"+new Date());
}

private void completeTask() {
try {
//assuming it takes 20 secs to complete the task
Thread.sleep(20000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

public static void main(String args[]){
TimerTask timerTask = new MyTimerTask();
//running timer task as daemon thread
Timer timer = new Timer(true);
timer.scheduleAtFixedRate(timerTask, 0, 10*1000);
System.out.println(\"TimerTask started\");
//cancel after sometime
try {
Thread.sleep(120000);
} catch (InterruptedException e) {
e.printStackTrace();
}
timer.cancel();
System.out.println(\"TimerTask cancelled\");
try {
Thread.sleep(30000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

}
Posted on Jul 26, 2014 by Johny Verma

Enter your Answer

Name
Email Address
Answer