Timer purge() method in Java with Examples
The purge() method of Timer class in Java is used to remove all the cancelled tasks from this queue of the Timer. The behaviour of the time remains unaffected by the calling of this method.
Syntax:
public int purge()
Parameters: The method does not take any parameters.
Return Value: The method returns the number of tasks that have been removed from the queue.
Below programs illustrate the working of purge() method in Java:
Program 1:
// Java code to illustrate purge() import java.util.*; public class Java_Timer_Demo { public static void main(String args[]) { // Creating the timer task, timer Timer time = new Timer(); TimerTask timetask = 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("Stopping the task"); time.cancel(); break; } } // purging the timer System.out.println("The Purge value:" + time.purge()); }; }; time.schedule(timetask, 1500, 2000); } } |
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 Stopping the task The Purge value:0
Program 2:
// Java code to illustrate purge() import java.util.*; public class Java_Timer_Demo { public static void main(String args[]) { // Creating the timer task, timer Timer time = new Timer(); TimerTask timetask = new TimerTask() { public void run() { for (int i = 1; i <= 5; i++) { System.out.println("Working on the task"); if (i >= 2) { System.out.println("Stopping the task"); time.cancel(); } } // Purging the timer System.out.println("The Purge value:" + time.purge()); }; }; time.schedule(timetask, 1, 1000); } } |
Working on the task Working on the task Stopping the task Working on the task Stopping the task Working on the task Stopping the task Working on the task Stopping the task The Purge value:0
Reference: https://docs.oracle.com/javase/9/docs/api/java/util/Timer.html#purge–
Recommended Posts:
- Timer cancel() method in Java with Examples
- Java.util.Timer Class in Java
- Java.lang.Short toString() method in Java with Examples
- Java.util.Collections.disjoint() Method in java with Examples
- Java lang.Long.numberOfLeadingZeros() method in Java with Examples
- Java lang.Long.lowestOneBit() method in Java with Examples
- Java lang.Long.byteValue() method in Java with Examples
- Java lang.Long.reverse() method in Java with Examples
- Java lang.Long.builtcount() method in Java with Examples
- Java.util.Collections.rotate() Method in Java with Examples
- Java lang.Long.highestOneBit() method in Java with Examples
- Java lang.Long.numberOfTrailingZeros() method in Java with Examples
- Java Clock tickMinutes() method in Java with Examples
- Java 8 | ArrayDeque removeIf() method in Java with Examples
- Java Clock withZone() 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.



