Timer schedule(TimerTask, Date) in Java

schedule(TimerTask, Date): This method is available in java.util.Timer class of Java.

Syntax:

void java.util.Timer.schedule(TimerTask task, Date time)

This method takes two arguments. This method schedules the specified task for execution at the specified time.

Parameters: Two parameters are required for this method.

task: task to be scheduled.

time: time at which task is to be executed.

Throws:

1. IllegalArgumentException - if time.getTime() is negative.

2. IllegalStateException - if task was already scheduled or canceled, the timer was canceled, or the timer thread terminated.

3. NullPointerException - if task or time is null

Approach 1: When no exception

Java

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class Timerschedule {
    public static void main(String[] args) {

        Timer timer = new Timer();

        TimerTask timerTask = new TimerTask() {

            @Override
            public void run() {
                System.out.println("Runner");

            }
        };
        Date date = new Date();
        timer.schedule(timerTask, date);

    }
}

Output:

Runner


Approach 2: IllegalArgumentException

Java

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class Timerschedule {
    public static void main(String[] args) {

        Timer timer = new Timer();

        TimerTask timerTask = new TimerTask() {

            @Override
            public void run() {
                System.out.println("Runner");

            }
        };
        Date date = new Date();
        date.setTime(-1000L);
        timer.schedule(timerTask, date);

    }
}

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:209)



Approach 3: IllegalStateException

Java

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class Timerschedule {
    public static void main(String[] args) {

        Timer timer = new Timer();

        TimerTask timerTask = new TimerTask() {

            @Override
            public void run() {
                System.out.println("Runner");

            }
        };
        Date date = new Date();
        timer.cancel();
        timer.schedule(timerTask, date);

    }
}

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:209)



Approach 4: NullPointerException 

Java

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class Timerschedule {
    public static void main(String[] args) {

        Timer timer = new Timer();

        TimerTask timerTask = new TimerTask() {

            @Override
            public void run() {
                System.out.println("Runner");

            }
        };
        Date date = null;
        timer.schedule(timerTask, date);

    }
}

Output:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.util.Date.getTime()" because "time" is null at java.base/java.util.Timer.schedule(Timer.java:209)


No comments:

Post a Comment