简体   繁体   English

如何在Java中停止计时器

[英]How to stop the timer in java

I have a timer method that runs everytime a user inputs something. 我有一个计时器方法,每次用户输入内容时都会运行。 Basically the user has a minute to input something or the timer will print a response, but it won't print a response if the user inputs something before the timer runs out of time. 基本上,用户有一分钟的时间来输入内容,否则计时器将打印响应,但是如果用户在计时器用完时间之前输入了内容,则不会打印响应。 Basically, I am having trouble cancelling the last timer and calling a new one, because if I don't cancel the timer, the previous timer keeps running while new one keep getting called so it just makes a huge mess. 基本上,我在取消最后一个计时器并调用新计时器时遇到了麻烦,因为如果我不取消计时器,则前一个计时器将继续运行,而新计时器将继续被调用,这只会造成很大的混乱。 My methods are below 我的方法如下

Main: 主要:

public static void main(String[] args) {
     while(true) {
          runner();
     }
}

Runner: 亚军:

public static void runner() {
     timer();
     String s = input.nextLine();
     System.out.println(s);

}

Timer: 计时器:

public static void timer() {
     TimerTask task = new TimerTask() {
           public void run() {
                System.out.println("Say something already!");
           }
    };
    long delay = TimeUnit.MINUTES.toMillis(1);
    Timer t = new Timer();
    t.schedule(task, delay);
}

How exactly will I make this cancel. 我到底要如何取消呢? Someone has to me to use the cancel() method in the TimerTask class but I don't understand where to call or even how. 我必须有人在TimerTask类中使用cancel()方法,但我不知道在哪里调用,甚至如何调用。

Have timer() method return the TimerTask object, and when input.nextLine() returns, call TimerTask.cancel(). 让timer()方法返回TimerTask对象,然后在input.nextLine()返回时调用TimerTask.cancel()。

public static void runner() {
     TimerTask t = timer();
     String s = input.nextLine();
     t.cancel();
     System.out.println(s);
}

public static void timer() {
     TimerTask task = new TimerTask() {
           public void run() {
                System.out.println("Say something already!");
           }
    };
    long delay = TimeUnit.MINUTES.toMillis(1);
    Timer t = new Timer();
    t.schedule(task, delay);
    return task;
}

Could also return the Timer instead and call Timer.cancel() 也可以返回Timer并调用Timer.cancel()

I guess this could work: 我想这可能有效:

Main:

TimerTask task;

public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);
    runner();

    while(true) {

        timer(); //run timer
        String line = scan.nextLine(); //read line (happens after hit Enter)
        System.out.println(line);
        //if line is read, then rerun timer which has on next iteration
    }

}

Timer:

public static void timer() {

    if(null != task) {
        task.cancel();
    }

    task = new TimerTask() {
           public void run() {
                System.out.println("Say something already!");
                //timer(); reinit maybe after 1 min again?
           }
    };

    long delay = TimeUnit.MINUTES.toMillis(1);
    Timer t = new Timer();
    t.schedule(task, delay);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM