java.util.Timer.cancel() Method

Advertisements


Description

The cancel() method is used to terminate this timer and discarding any currently scheduled tasks.

Declaration

Following is the declaration for java.util.Timer.cancel() method.

public void cancel()

Parameters

  • NA

Return Value

NA

Exception

  • NA

Example

The following example shows the usage of java.util.Timer.cancel()

package com.tutorialspoint;

import java.util.*;

public class TimerDemo {
   public static void main(String[] args) {
      // creating timer task, timer
      TimerTask tasknew = new TimerCancel();
      Timer timer = new Timer();
      
      // scheduling the task
      timer.scheduleAtFixedRate(tasknew, new Date(), 100);      
      
      // terminating the timer
      System.out.println("cancelling timer");
      timer.cancel();
   }
   // this method performs the task
   public void run() {
      System.out.println("working on");      
   }    
}

Let us compile and run the above program, this will produce the following result.

cancelling timer
working on



Advertisements
Advertisements