schedule(TimerTask, Date, long): This method is available in java.util.Timer class of Java.
Syntax:
void java.util.Timer.schedule(TimerTask task, Date firstTime, long period)
This method takes three arguments. This method schedules the specified task for repeated fixed-delay execution, beginning at the specified time.
Note: Subsequent executions take place at approximately regular intervals, separated by the specified period.
Parameters: Three parameters are required for this method.
task: task to be scheduled.
firstTime: First time at which task is to be executed.
period: time in milliseconds between successive task executions.
Throws:
1. IllegalArgumentException - if firstTime.getTime() < 0, or period <= 0.
2. IllegalStateException - if task was already scheduled or cancelled, timer was cancelled, or timer thread terminated.
3. NullPointerException - if task or firstTime is null
Approach 1: When no exception
Java
import java.util.Date;import java.util.Timer;import java.util.TimerTask;public class Timerschedule3 {public static void main(String[] args) {Timer timer = new Timer();TimerTask timerTask = new TimerTask() {@Overridepublic void run() {System.out.println("Runner");}};Date firtTime = new Date();long period = 10000L;timer.schedule(timerTask, firtTime, period);}}
Output:
Runner Runner......
Approach 2: IllegalArgumentException
Java
import java.util.Date;import java.util.Timer;import java.util.TimerTask;public class Timerschedule3 {public static void main(String[] args) {Timer timer = new Timer();TimerTask timerTask = new TimerTask() {@Overridepublic void run() {System.out.println("Runner");}};Date firtTime = new Date();firtTime.setTime(-100L);long period = 10000L;timer.schedule(timerTask, firtTime, period);}}
Output:
Exception in thread "main" java.lang.IllegalArgumentException: Illegal execution time. at java.base/java.util.Timer.sched(Timer.java:389) at java.base/java.util.Timer.schedule(Timer.java:288)
Approach 3: IllegalStateException
Java
import java.util.Date;import java.util.Timer;import java.util.TimerTask;public class Timerschedule3 {public static void main(String[] args) {Timer timer = new Timer();TimerTask timerTask = new TimerTask() {@Overridepublic void run() {System.out.println("Runner");}};Date firtTime = new Date();long period = 10000L;timer.cancel();timer.schedule(timerTask, firtTime, period);}}
Output:
Exception in thread "main" java.lang.IllegalStateException: Timer already cancelled. at java.base/java.util.Timer.sched(Timer.java:398) at java.base/java.util.Timer.schedule(Timer.java:288)
Approach 4: NullPointerException
Java
import java.util.Date;import java.util.Timer;import java.util.TimerTask;public class Timerschedule3 {public static void main(String[] args) {Timer timer = new Timer();TimerTask timerTask = new TimerTask() {@Overridepublic void run() {System.out.println("Runner");}};Date firtTime = null;long period = 10000L;timer.schedule(timerTask, firtTime, period);}}
Output:
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.util.Date.getTime()" because "firstTime" is null at java.base/java.util.Timer.schedule(Timer.java:288)
No comments:
Post a Comment