• Home
  • Testing
  • SAP
  • Web
  • Must Learn!
  • Big Data
  • Live Projects
  • Blog
Ways of Timer Java implementation
    1) To set up a specific amount of delay until a task is executed.
    2) To find the time difference between two specific events

Java Timer - Need to understand Few concepts
  • It can only be a future event that can be timed.
  • Will the event occur once or repeatedly?
  • How long is the Timer required?
  • There may be several Timers required in parallel.
  • The timers should have the facility to stop or even cancel at any given point.

How to use Java Timer and Example

Java Timer Example

Using the Java timer for your programs

The Timer in java is provided within the java.util package.

Let us have a look at using Timer for 2 tasks that we need to schedule-

Step 1) But before that lets create the class that will contain some task that we need to do-

importjava.util.TimerTask;

importjava.util.Timer;

public class TaskMaster extends TimerTask{

       String strObject;

       publicTaskMaster(String strObject){

           this.strObject = strObject;

       }

       public void run(){

           System.out.println("Inside Run task-" + strObject);

       }
}

This is a simple class that uses the run() method of Threads when the task is invoked. The String strObject will help us identify which task is running.

Step 2) Create 2 timers as follows in a class called Java Timer class:

Timer timer_1;

Timer timer_2;

Step 3) Create a constructor of this class having 2 integer parameters as input-

public Timers(int t1,int t2) {
}

This constructor will have the code to invoke the 2 timers declared earlier to use for the tasks we created in TaskMaster class.

Step 4) Initiate the timers as follows –

timer_1 = new Timer();

Similarly do this for timer_2.

Step 5) Now if you use this timer object, you will see several methods in it.

We will first make use of the schedule method that takes in arguments as –

task - task to be scheduled.

delay - delay in milliseconds before task is to be executed.

period - time in milliseconds between successive task executions.

The delay and period will be the 2 integer arguments that we have passed in the constructor (created in step 3).

So the constructor now has the code as

timer_1 = new Timer();

timer_1.schedule(new TaskMaster("Alpha"), t1 * 1000, t1 * 1000);

Step 6) Repeat step 5 for timer_2 and pass some different string say “Delta”.

Your constructor should now be looking something like-

public Timers(int t1,int t2) {

       timer_1 = new Timer();

       timer_1.schedule(new TaskMaster("Alpha"), t1 * 1000, t1 * 1000);

       //timer_1.scheduleAtFixedRate(new TaskMaster("Alpha"), t1 * 1000, t1 * 1000);

       timer_2 = new Timer();

       timer_2.schedule(new TaskMaster("Delta"), t2 * 1000, t2 * 1000);

       //timer_2.scheduleAtFixedRate(new TaskMaster("Delta"), t2 * 1000, t2 * 1000);

}

Ignore the commented lines for now. We shall see that after executing this code in step 8.

Step 7) Now create the main method and call the Timersclass with 2 arguments as integers.

public static void main(String args[]) {

new Timers(1,5);

}

  Step 8) On executing this main method, the result will appear something like this-

Inside Run task-Alpha

Inside Run task-Alpha

Inside Run task-Alpha

Inside Run task-Alpha

Inside Run task-Delta

Inside Run task-Alpha

Inside Run task-Alpha

Inside Run task-Alpha

Inside Run task-Alpha

Inside Run task-Alpha

Inside Run task-Alpha

Inside Run task-Delta

Inside Run task-Alpha

Inside Run task-Alpha

Inside Run task-Alpha

…… This will continue until you don’t terminate the program.

This was the Schedule method of timer that schedules the specified task for repeated fixed-delay execution, beginning after the specified delay. Subsequent executions take place at approximately regular intervals separated by the specified period.

In fixed-delay execution, each execution is scheduled relative to the actual execution time of the previous execution

Step 9) Now comment the line with schedule method and uncomment the scheduleAtFixedRate.

The output will be quite similar.

This method too schedules the specified task for repeated fixed-rate execution, beginning after the specified delay. Subsequent executions take place at approximately regular intervals, separated by the specified period.

In fixed-rate execution, each execution is scheduled relative to the scheduled execution time of the initial execution.

Oh yes!! You can programmatically stop the timers by calling the cancel() method of the timer.

 

YOU MIGHT LIKE: