Java TimerTask cancel() Method

The cancle() is the method of TimerTask class. This method is used to stop the timer task if the task is scheduled for 1-time execution or has not been run. It will never run again if the task has been scheduled for repeated execution.

Syntax

Return

It returns true if the task is scheduled for 1-time execution and has not yet run and returns false if the task was scheduled for 1-time execution and has already run,

Example 1

Output:

Task Timer is on 
Task Timer is on 
Task Timer is on 
Task Timer is on 
Task Timer is on 
Task Timer is on 
Task Timer is on 
Task Timer is on 
Task Timer is on 
Task Timer is on 
Task Timer cancel: true

Example 2

Output:

Task Timer is on 
Task Timer cancel: true