xNos

Untitled

May 23rd, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.66 KB | None | 0 0
  1. import java.util.Timer;
  2. import java.util.TimerTask;
  3.  
  4. /**
  5.  * Simple demo that uses java.util.Timer to schedule a task
  6.  * to execute once 5 seconds have passed.
  7.  */
  8.  
  9. public class Reminder {
  10.     Timer timer;
  11.  
  12.     public Reminder(int seconds) {
  13.         timer = new Timer();
  14.         timer.schedule(new RemindTask(), seconds*1000);
  15.     }
  16.  
  17.     class RemindTask extends TimerTask {
  18.         public void run() {
  19.             System.out.println("Time's up!");
  20.             timer.cancel(); //Terminate the timer thread
  21.         }
  22.     }
  23.  
  24.     public static void main(String args[]) {
  25.         new Reminder(5);
  26.         System.out.println("Task scheduled.");
  27.     }
  28. }
Add Comment
Please, Sign In to add comment