Timer cancel() method in Java with Examples
The cancel() method of Timer class is used to terminate this timer and remove any currently scheduled tasks.
Syntax:
public void cancel()
Parameters: The function does not accepts any parameter.
Return Value: The method has no return value.
Exception: The function does not throws any exception.
Program below demonstrates the above mentioned function:
Program 1:
// program to demonstrate the // function java.util.Timer.cancel() import java.util.*; public class GFG { public static void main(String[] args) { // creating timertask, timer Timer timer = new Timer(); TimerTask tt = new TimerTask() { public void run() { for (int i = 1; i <= 15; i++) { System.out.println("working on the task"); if (i >= 7) { System.out.println("stop the task"); // loop stops after 7 iterations timer.cancel(); break; } } }; }; timer.schedule(tt, 1000, 1000); } } |
chevron_right
filter_none
Output:
working on the task working on the task working on the task working on the task working on the task working on the task working on the task stop the task
Program 2:
// program to demonstrate the // function java.util.Timer.cancel() import java.util.*; public class GFG { public static void main(String[] args) { // creating timertask, timer Timer timer = new Timer(); TimerTask tt = new TimerTask() { public void run() { for (int i = 1; i <= 15; i++) { System.out.println("working on the task"); if (i >= 7) { System.out.println("stop the task"); // loop stops after 7 iterations timer.cancel(); } } }; }; timer.schedule(tt, 1000, 1000); } } |
chevron_right
filter_none
Output:
working on the task working on the task working on the task working on the task working on the task working on the task working on the task stop the task working on the task stop the task working on the task stop the task working on the task stop the task working on the task stop the task working on the task stop the task working on the task stop the task working on the task stop the task working on the task stop the task
Recommended Posts:
- Timer purge() method in Java with Examples
- Java.util.Timer Class in Java
- Java lang.Long.numberOfLeadingZeros() method in Java with Examples
- Java lang.Long.numberOfTrailingZeros() method in Java with Examples
- Java lang.Long.lowestOneBit() method in Java with Examples
- Java lang.Long.builtcount() method in Java with Examples
- Java lang.Long.reverse() method in Java with Examples
- Java lang.Long.byteValue() method in Java with Examples
- Java lang.Long.highestOneBit() method in Java with Examples
- Java.lang.Short toString() method in Java with Examples
- Java.util.Collections.rotate() Method in Java with Examples
- Java.util.Collections.disjoint() Method in java with Examples
- Java Clock tickMinutes() method in Java with Examples
- Java Clock withZone() method in Java with Examples
- Java 8 | ArrayDeque removeIf() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


