Timer purge() method in Java with Examples Last Updated : 29 May, 2019 Comments Improve Suggest changes Like Article Like Report 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 // 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); } } 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 Stopping the task The Purge value:0 Program 2: Java // 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); } } Output: 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-- Comment More infoAdvertise with us Next Article Timer purge() method in Java with Examples C chinmoy lenka Follow Improve Article Tags : Misc Java Java - util package Java-Functions Java-Timer +1 More Practice Tags : JavaMisc Similar Reads TimeUnit sleep() method in Java with Examples The sleep() method of TimeUnit Class is used to performs a Thread.sleep using this time unit. This is a convenience method that sleeps time arguments into the form required by the Thread.sleep method. Syntax: public void sleep(long timeout) throws InterruptedException Parameters: This method accepts 2 min read 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. Prog 2 min read OffsetTime of() method in Java with Examples The of(int hour, int minute, int second, int nanosecond, ZoneOffset offset) method of the OffsetTime class in Java is used to create an instance of OffsetTime from the passed values of hour, minute, second and nanosecond. In this method, we pass the value of hour, minute, second and nanosecond in in 2 min read OffsetTime minus() method in Java with examples The minus(amountToSubtract, unit) method of OffsetTime class in Java returns a copy of this time with the specified amount subtracted. Syntax : public OffsetTime minus(long amountToSubtract, TemporalUnit unit) Parameter: The method accepts two parameters which are described below: amountToSubtract: 3 min read OffsetTime now() method in Java with examples The now() method of OffsetTime class in Java obtains the current time from the system clock in the default time-zone. Syntax : public static OffsetTime now() Parameter: This method does not accepts any parameter. Return Value: It returns the current time using the system clock and default time-zone 2 min read Like