Recurring Countdown Timer in Java

前端 未结 4 1028
自闭症患者
自闭症患者 2021-01-21 21:49

I\'m trying to implement a countdown timer into a pre-existing public class and I have a few questions.

An overview: I want to have a timer within a program that counts

4条回答
  •  走了就别回头了
    2021-01-21 22:19

    You could use java.util.Timer to schedule an execution of a method and then cancel it if the requirements is met.

    Like this:

    timer = new Timer();
    timer.schedule(new Task(), 60 * 1000);
    

    And then make a class like this to handle the timerschedule:

    class Task extends TimerTask {
        public void run() {
          System.exit(0);
        }
      }
    

    If the requirements is met, then do this to stop it from executing:

    timer.cancel();
    

提交回复
热议问题