Illuminatiiiiii
6/7/2019 - 6:52 AM

Java Library - Timer and TimerTask

package me.illuminatiproductions;

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

//The class that represents our task
class EatPeopleTask extends TimerTask{
    public void run(){
        System.out.println("*eat all the people*");
        System.out.println("End of task.");
    }
}

public class Main {

    public static void main(String[] args) {

        EatPeopleTask task = new EatPeopleTask();
        Timer timer = new Timer();

        timer.schedule(task, 5000); //Executes task 5 seconds after the program starts

        //remember, the timer is a thread that is running concurrent with the main thread of the program
        try{
            Thread.sleep(5000); //After the timer starts, pause the main thread for 5 seconds
        }catch (InterruptedException exc){ }

        timer.cancel(); //then end the program because the thread
        // is done so there is nothing else to do

    }
}