Timer schedule(TimerTask, long) in Java

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

Syntax:

void java.util.Timer.schedule(TimerTask task, long delay)

This method takes two arguments. This method schedules the specified task for execution after the specified delay.

Parameters: Two parameters are required for this method.

task: task to be scheduled.

delay: delay in milliseconds before task is to be executed.

Throws:

1. IllegalArgumentException - if delay is negative, or delay + System.currentTimeMillis() is negative.

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

3. NullPointerException - if the task is null

Approach 1: When no exception

Java

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

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

        Timer timer = new Timer();

        TimerTask timerTask = new TimerTask() {

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

            }
        };
        long delay = 10000L;
        timer.schedule(timerTask, delay);

    }
}

Output:

Runner


Approach 2: IllegalArgumentException 

Java

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

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

        Timer timer = new Timer();

        TimerTask timerTask = new TimerTask() {

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

            }
        };
        long delay = -10000L;
        timer.schedule(timerTask, delay);

    }
}

Output:

Exception in thread "main" java.lang.IllegalArgumentException: Negative delay. at java.base/java.util.Timer.schedule(Timer.java:193)



Approach 3: IllegalStateException 

Java

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

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

        Timer timer = new Timer();

        TimerTask timerTask = new TimerTask() {

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

            }
        };
        long delay = 10000L;
        timer.cancel();
        timer.schedule(timerTask, delay);

    }
}

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



Approach 4: NullPointerException

Java

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

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

        Timer timer = new Timer();

        TimerTask timerTask = null;
        long delay = 10000L;

        timer.schedule(timerTask, delay);

    }
}

Output:

Exception in thread "main" java.lang.NullPointerException: Cannot read field "lock" because "task" is null at java.base/java.util.Timer.sched(Timer.java:400) at java.base/java.util.Timer.schedule(Timer.java:194)


No comments:

Post a Comment