Timers in Logo

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

public class Timers {
    public static void main(String[] args) {
        // Timers represent a single event in the future. You
        // tell the timer how long you want to wait, and it
        // provides a callback that will be executed at that
        // time. This timer will wait 2 seconds.
        Timer timer1 = new Timer();
        timer1.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("Timer 1 fired");
            }
        }, 2000);

        // Wait for the timer to complete
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // If you just wanted to wait, you could have used
        // Thread.sleep(). One reason a timer may be useful is
        // that you can cancel the timer before it fires.
        // Here's an example of that.
        Timer timer2 = new Timer();
        TimerTask task2 = new TimerTask() {
            @Override
            public void run() {
                System.out.println("Timer 2 fired");
            }
        };
        timer2.schedule(task2, 1000);

        boolean cancelled = task2.cancel();
        if (cancelled) {
            System.out.println("Timer 2 stopped");
        }

        // Give the timer2 enough time to fire, if it ever
        // was going to, to show it is in fact stopped.
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // Clean up the timers
        timer1.cancel();
        timer2.cancel();
    }
}

In Java, we use the Timer and TimerTask classes to schedule tasks for future execution. Here’s how the example works:

  1. We create a Timer object and schedule a task to run after 2 seconds using timer1.schedule(). The task is defined as an anonymous inner class extending TimerTask.

  2. We use Thread.sleep() to wait for the first timer to complete. In Java, we need to handle the InterruptedException that sleep() might throw.

  3. For the second timer, we create a separate TimerTask object. This allows us to cancel the task before it fires.

  4. We schedule the second task to run after 1 second, but immediately try to cancel it using task2.cancel().

  5. We sleep for 2 seconds again to give the second timer enough time to fire if it wasn’t successfully cancelled.

  6. Finally, we cancel both timers to clean up resources.

To run this program:

$ javac Timers.java
$ java Timers
Timer 1 fired
Timer 2 stopped

The first timer will fire approximately 2 seconds after we start the program, but the second should be stopped before it has a chance to fire.

Note that Java’s Timer class is older and has some limitations. For more complex scheduling tasks, consider using the ScheduledExecutorService from the java.util.concurrent package, which provides more flexibility and better handles concurrent execution.